xixiiii 2014年01月13日
表单信息保存到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 IOException, ServletException {
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>");
out.println("<head>");
out.println("<title>你好</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1 align=center>你好!" + name + "</h1>");
out.println("</body>");
out.println("</html>");
}

}

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app id ="WebApp_ID">
<display-name>javaee1</display-name>

<servlet>
<servlet-name>Register</servlet-name>
<servlet-class>servlet.Register</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Register</servlet-name>
<url-pattern>/servlet/register</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

</web-app>


...全文
138 点赞 收藏 11
写回复
11 条回复

还没有回复,快来抢沙发~

发动态
发帖子
Java EE
创建于2007-09-28

3.7w+

社区成员

22.5w+

社区内容

J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区公告
暂无公告