[求助 servlet] 页面无法跳转
一个简单的用户登录验证网页,但是无法实现跳转
下面是源码。
登录界面:
//登陆界面package com.arlen.java.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;public class Login extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {// TODO Auto-generated method stub//中文乱码resp.setContentType("text/html; charset=gbk");PrintWriter pw = resp.getWriter();pw.println("<html>");pw.println("<head>");pw.println("<title> Login Page </title>");pw.println("</head>");pw.println("<body><center>");pw.println("<h1>登录界面</h1><hr>");pw.println("<form action=login_analyse method=post>");pw.println("姓名:<input type=text name=userName><br><br>");pw.println("密码:<input type=password name=passwd><br><br>");pw.println("<input type=submit value=login><br><hr>");pw.println("</center></body>");pw.println("</html>");pw.println("");pw.println("");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {// TODO Auto-generated method stub}}
?登录分析界面
?
package com.arlen.java.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class LoginAnalyse extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {// TODO Auto-generated method stub// 接收姓名和密码String name = req.getParameter("userName");String pwd = req.getParameter("passwd");if (name.equals("abc") && pwd.equals("123")) {resp.sendRedirect("login");} else {resp.sendRedirect("welcome");}}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {// TODO Auto-generated method stub}}
?登陆成功后转到的欢迎界面
?
package com.arlen.java.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;public class Welcome extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {// TODO Auto-generated method stub// 中文乱码resp.setContentType("text/html; charset=gbk");PrintWriter pw = resp.getWriter();pw.println("<html>");pw.println("<head>");pw.println("<title> Welcome </title>");pw.println("</head>");pw.println("<body><center>");pw.println("<h1>Welcome</h1><hr>");pw.println("</center></body>");pw.println("</html>");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {// TODO Auto-generated method stub}}
?web.xml配置
?
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="WebApp_ID" version="3.0"><display-name>MyWebSite</display-name><description>Welclme to MyWebSite</description><!-- JSPC servlet mapping start --><!-- Login --><servlet><!-- 给你的servlet取名,任意 --><servlet-name>Login</servlet-name><!-- 指明servlet的路径(包名+类名) --><servlet-class>com.arlen.java.servlet.Login</servlet-class></servlet><servlet-mapping><!-- 给你的servlet取名,任意 --><servlet-name>Login</servlet-name><!-- 在浏览器中输入的访问该servlet的url,任意 --><url-pattern>/login</url-pattern></servlet-mapping><!-- Login Analyse --><servlet><!-- 给你的servlet取名,任意 --><servlet-name>LoginAnalyse</servlet-name><!-- 指明servlet的路径(包名+类名) --><servlet-class>com.arlen.java.servlet.LoginAnalyse</servlet-class></servlet><servlet-mapping><!-- 给你的servlet取名,任意 --><servlet-name>LoginAnalyse</servlet-name><!-- 在浏览器中输入的访问该servlet的url,任意 --><url-pattern>/login_analyse</url-pattern></servlet-mapping><!-- Login Analyse --><servlet><!-- 给你的servlet取名,任意 --><servlet-name>Welcome</servlet-name><!-- 指明servlet的路径(包名+类名) --><servlet-class>com.arlen.java.servlet.Welcome</servlet-class></servlet><servlet-mapping><!-- 给你的servlet取名,任意 --><servlet-name>Welcome</servlet-name><!-- 在浏览器中输入的访问该servlet的url,任意 --><url-pattern>/welcome</url-pattern></servlet-mapping><!-- JSPC servlet mapping end --><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>
?
?