Servlet案例三

来源:互联网 发布:卖男装的淘宝店铺 编辑:程序博客网 时间:2024/06/15 23:33




WebContent-文件夹14下 新建 success.jsp  和 error.jsp
success.jsp文件内容如下

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!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>登陆成功提示页面</title>

</head>

<body>

 登陆成功。<br/>

 您提交的信息为:<br/>

 用户名:<%= request.getParameter("uname"%><br/>

 密码:<%= request.getParameter("upwd"%><br/>

 <a href="login.jsp">返回登陆页面</a>

</body>

</html>


error.jsp文件内容如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!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>登陆议程提示页面</title>

</head>

<body>

 登陆失败。<br/>

 您提交的信息为:<br/>

 用户名:<%= request.getParameter("uname"%><br/>

 密码:<%= request.getParameter("upwd"%><br/>

 <a href="login.jsp">返回登陆页面</a>

</body>

</html>





修改LoginServlet.java中的doPost方法

@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

System.out.println("======进入doPost方法======");

        String userName = req.getParameter("uname");

        String password = req.getParameter("upwd");

        

        System.out.println("用户名 ==》 " + userName);

        System.out.println("密码 ==》 " + password);

        

        if (userName.equals("darkmi") && password.equals("jikexueyuan")) {

        resp.sendRedirect(req.getContextPath() + "/14/success.jsp");//sendRedirect页面的重定向,无法获取之前提交的表单数据

} else {

resp.sendRedirect(req.getContextPath() + "/14/error.jsp");

}

}


web.xml文件不变

重启Tomcat 打开浏览器http://localhost:8080/JSPStudy/14/login.jsp



当输入用户名为darkmi并且密码为jikexueyuan   跳转到 http://localhost:8080/JSPStudy/14/success.jsp

登陆成功。
您提交的信息为:
用户名:null
密码:null
返回登陆页面

否则跳转到http://localhost:8080/JSPStudy/14/error.jsp



打开FireFox浏览器http://localhost:8080/JSPStudy/14/login.jsp  工具-Web开发者--网络

响应头Location字段 "/JSPStudy/14/success.jsp"





0 0