本文介绍了request.getParameter()返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

得到了一个家庭作业,这给我带来了问题....它通过添加两个页面,一个控制器servlet和另一个用于两个页面的bean来修改具有两个页面和一个Bean的JSF项目以适合MVC2.新的主页将转发到第二个新页面或旧的第一页.我的问题是response.getParameter()始终为null.

Got a homework assignment that is giving me problems.... Its modifying a JSF project with two pages and a bean to fit MVC2 by adding two more pages and a controller servlet and another bean for the two additional pages. the new main page forwards to either the second new page or the old first page. My issue is response.getParameter() always results in null.

<%@page session="false" import="java.util.Iterator"%>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<jsp:useBean id="status" scope="request" class="JSFRegistration.Status" />
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>JSP Page</title>
    </head>
    <body>
        <% if (status!=null && !status.isSuccessful()){%>
            <font color="red">Processing errors:
                <ul><%Iterator errors=status.getExceptions();
            while (errors.hasNext()){
                Exception e = (Exception) errors.next();%>
                <li><%= e.getMessage()%><%}%></ul></font><%}%>
        <form action="LoginServlet" method="POST">
            <% String username = request.getParameter("username");
            if (username==null) username="";%>
            <input type="text" name="usernameTF" value="<%=username%>" />
            <% String password = request.getParameter("password");
            if (password==null) password="";%>
            <input type="password" name="passwordTF" value="<%=password%>" />
            <input type="submit" value="Login" />
        </form>

    </body>
</html>

这基本上是我们书中的直接副本,但我需要用于新主页的字段.与控制器Servlet相同,直接复制除了只包含我需要的字段.

this is basically a direct copy from our book but the fields I need for the new main page. Same for the controller servlet, a direct copy except only contains the fields I need.

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    RequestDispatcher view = null;
    Status status = new Status();
    request.setAttribute("status", status);
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    if (username==null || username.length()==0)
        status.addException(new Exception("Please enter username"));
    if (password==null)
        status.addException(new Exception("Please enter password"));
    if (!status.isSuccessful()){
        view = request.getRequestDispatcher("Login.jsp");
        //view.forward(request, response);
    }
    else
        try{
            request.setAttribute("username", username);
            request.setAttribute("password", password);
        view = request.getRequestDispatcher("Registration.jsp");
        } catch (Exception e) {
            status.addException(new Exception("Error"));
            view = request.getRequestDispatcher("Login.jsp");
        }
    view.forward(request, response);
}

和Status类,这也是本书的直接副本.

and the Status class, again a direct copy from the book.

public class Status {
private ArrayList exceptions;

public Status(){
    exceptions = new ArrayList();
}
public void addException(Exception exception) {
    exceptions.add(exception);
}
public boolean isSuccessful(){
    return (exceptions.isEmpty());
}
public Iterator getExceptions(){
    return exceptions.iterator();
}

无论在两个框中键入什么内容,单步执行调试都会显示值未传递给参数.如果两个字段都具有文本,如果只有一个字段具有文本,并且两个字段都为空,则会在屏幕上方为两个字段打印创建的例外.

regardless of what is typed into the two boxes, stepping through a debug shows the values not getting passed to the parameters. I get the created exceptions printed above the screen for both fields if both have text, if only one has text and when both are empty.

推荐答案

您的请求参数名称与输入字段名称不匹配.您已经为输入字段分配了名称usernameTFpasswordTF.然后,可以使用那些名称作为请求参数来使用它们,但是您正在尝试使用名称usernamepassword来获取它们.因此,您需要修复输入字段名称或请求参数名称,以使它们彼此匹配.

Your request parameter names do not match the input field names. You've assigned the input fields a name of usernameTF and passwordTF. They are then available by exactly those names as request parameter, but you're attempting to get them using the names username and password. So you need either to fix the input field names, or the request parameter names so that they match each other.

顺便说一句,为什么从现代的MVC框架(如JSF)退回到笨拙的带有混合业务代码的90年代风格的JSP?真的是 作业要求的吗?另外,自1998年以来,不推荐使用HTML <font>元素.您从哪里学到的?课程的质量真的很好吗?

By the way, why falling back from a modern MVC framework like JSF to awkward 90's style JSP with mingled business code? Is that really what the homework assignment is asking you? Also the HTML <font> element is deprecated since 1998. Where did you learn about it? Is the quality of the course really good?

这篇关于request.getParameter()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 01:13