日期:2014-05-18  浏览次数:20742 次

表单信息保存到session,跳转到servlet,一直报404错误
自学servlet中。做一个简单的表单提交,将信息保存到session中并查看,表单提交没有问题,但是跳转到/servlet/testSession 一直是404错误。。求大神帮忙解决!!!

包servlet下类Register:

package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class Register extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOExceptionServletException {
request.setCharacterEncoding("GBK");

String name = request.getParameter("name");
String pass = request.getParameter("pass");
String gender = request.getParameter("gender");
String[] hobby = request.getParameterValues("hobby"); 
String class1 = request.getParameter("class");
String introduce = request.getParameter("introduce");
String s_hobby = "";
if (hobby != null) {
for (int i = 0; i < hobby.length; i++) {
s_hobby = s_hobby + hobby[i];
if (i < hobby.length - 1) {
s_hobby = s_hobby + ",";
}
}
}
HttpSession session = request.getSession();
session.setAttribute("name", name);
response.setContentType("text/html;charset=GBK");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>处理表单</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1 align=center>你好!</h1>");
out.println("<p>姓名:" + name);
out.println("<p>密码:" + pass);
out.println("<p>性别:" + gender);
out.println("<p>爱好:" + s_hobby);
out.println("<p>班级:" + class1);
out.println("<p>自我介绍:");
out.println("<p>" + introduce);
out.println("</body>");
out.println("</html>");
out.println("<p><a href='testSession'>测试session</a>");
}
}

包servlet下类TestSession:

package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class TestSession extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
String name = (String)session.getAttribute("name");
response.setContentType("text/html;charset=GBK");
PrintWriter out = response.getWriter();
out.println("<html>");