request.getParameter() returns null

2019-09-15 01:40发布

问题:

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>

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);
}

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.

回答1:

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.

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?