问答详情
源自:-

用servlet获取表单中显示这一步request.getRequestDispatcher("../userinfo.jsp").forward(request, response);出问题

reg.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Register</h1>
<hr>
<form name="regForm" action="servlet/RegSevlet" method="post"></form>
<table border = "0" width ="800" cellspacing="0" >
<tr>
<td class = "lable">Name:</td>
<td class = "controler"><input type="text" name="username"></td>
</tr><tr>
<td class = "lable">Password:</td>
<td class = "controler"><input type="password" name="mypassword"></td>
</tr>
<tr>
<td class = "lable">Confirm:</td>
<td class = "controler"><input type="password" name="confirmpass"></td>
</tr>
<tr>
<td class = "lable">Email:</td>
<td class = "controler"><input type="text" name="email"></td>
</tr>
<tr>
<td class = "lable">Gender:</td>
<td class = "controler"><input type="radio" name="gender" checked="checked" value="Male">Male
<input type="radio" name = "gender" checked="checked" value="Female">Female</td>
</tr>
<tr>
<td class = "lable">Hobby:</td>
<td class = "controler"><input type="checkbox" name="favorites" value="nba"> NBA;
<input type="checkbox" name="favorites" value="music">Music;
<input type ="checkbox" name ="favorites" value="movie">Movie;
<input type="checkbox" name="favorites" value="internet">internet;</td>
</tr>
<tr>
<td class = "lable">Introduce:</td>
<td class = "controler"><textarea name="introduce" rows="10" cols="40"></textarea>
</tr>
</table>
<form action="servlet/RegServlet" method="post">
  <input type="submit" value="PostTest"/>
 
</form>

</body>
</html>

-----------------------

userinfo.jsp

<%@ page language="java" import="java.util.*,java.text.*" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>UserInfo</title>
</head>
<body>
<h1>User Info</h1>
<center>
<jsp:useBean id="regUser" class="entity.Users" scope="session"/>
<table cellpadding="0" cellspacing="0" border="1">
<tr>
<td class ="title">Name</td>
<td class ="content"><jsp:getProperty property="username" name="regUser"/></td>
</tr>
<tr>
<td class ="title">Password</td>
<td class ="content"><jsp:getProperty property="mypassowrd" name="regUser"/></td>
</tr>
<tr>
<td class ="title">Gender</td>
<td class ="content"><jsp:getProperty property="gender" name="regUser"/></td>
</tr>
<tr>
<td class ="title">Email</td>
<td class ="content"><jsp:getProperty property="email" name="regUser"/></td>
</tr>
<tr>
<td class ="title">Hobby</td>
<td class ="content"></td>
<%
String[] favorites = regUser.getFavorites();
for(String f:favorites){
%>
<%=f%>
<%
 }
%>
</tr>
<tr>
<td class ="title">Introduce</td>
<td class ="content"><jsp:getProperty property="introduce" name="regUser"/></td>
</tr>
</table>
</center>
</body>
</html>

------------------------

RegServlet.java

package servlet;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entity.Users;

/**
 * Servlet implementation class RegServlet
 */
@WebServlet("/RegServlet")
public class RegServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see Servlet#init(ServletConfig)
     */
    public void init(ServletConfig config) throws ServletException {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);// TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        Users u =new Users();
        String username,mypassword,gender,email,introduce,flag;
        String[] favorites;
        try
        {
            username= request.getParameter("username");
            mypassword= request.getParameter("mypassword");
            gender= request.getParameter("gender");
            email= request.getParameter("email");
            introduce= request.getParameter("introduce");
            favorites= request.getParameterValues("favorites");
            flag= request.getParameter("flag");
            
            u.setUsername(username);
            System.out.println("username");
            u.setMypassword(mypassword);
            System.out.println("p");
            u.setGender(gender);
            System.out.println("g");
            u.setEmail(email);
            System.out.println("e");
            u.setFavorites(favorites);
            System.out.println("f");
            u.setIntroduce(introduce);
            System.out.println("in");
            
            request.getSession().setAttribute("regUser", u);
            
            request.getRequestDispatcher("../userinfo.jsp").forward(request, response);
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }
}

------------------------

不知道怎么解决,尝试了好多路径

username
p
g
e
f
in
[err] java.lang.NullPointerException
[err]     at servlet.RegServlet.doPost(RegServlet.java:75)
[err]     at javax.servlet.http.HttpServlet.service(HttpServlet.java:595)
[err]     at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
[err]     at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1285)
[err]     at [internal classes]
[err]     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1157)
[err]     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:627)
[err]     at java.lang.Thread.run(Thread.java:801)

提问者:chocokids烨_ 2016-02-08 11:37

个回答

  • chocokids烨_
    2016-02-13 21:06:16

    问题解决了,发现是拼写错误。