通过一个登陆程序来理解几个jsp内置对象的使用

来源:互联网 发布:淘宝拍卖可以退货吗 编辑:程序博客网 时间:2024/05/16 09:42

最近在学西jsp的内置对象,这个程序是通过对登陆程序的模拟来学习使用功能jsp的内置对象。其中涉及到session,request,response的基本使用,以及服务器内部转发和重定向的实现
环境,eclipse,没有涉及数据库

登陆界面

<%@ 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>    <h1>这是一个登陆界面的测试程序</h1>    <hr />    <form action="dologin.jsp" method="post" >        <table>            <tr>                <td>用户名:</td>                <td><input type="text" name="username"/></td>            </tr>            <tr>                <td>密码:</td>                <td><input type="password" name="password" /></td>            </tr>            <tr>                <td colspan="2"><input type="submit" name="submit"/></td>            </tr>        </table>    </form></body></html>

处理界面

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>    <%        String username="";        String password="";        request.setCharacterEncoding("utf-8");//防止中文乱码        //用request方法获取用户名,密码        username=request.getParameter("username");        password=request.getParameter("password");        //如果用户名密码均为admin则登陆成功        if("admin".equals(username)&&"admin".equals(password))        {            //用服务器内部转发的方式转到登陆成功的页面,将request对象和response对象向后传            session.setAttribute("loginName", username);//用session保存登陆的用户名            request.getRequestDispatcher("login_success.jsp").forward(request, response);        }        else        {            //登陆失败就请求重定向到登陆失败页面            response.sendRedirect("login_failure.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>    <h1>登陆成功</h1> <hr />    <%    String loginuser="";    if(session.getAttribute("loginName")!=null)    {        loginuser=session.getAttribute("loginName").toString();    }    %>    欢迎<font color="red"><%=loginuser %></font></body></html>

登陆失败界面

<%@ 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>    <h1>登陆失败</h1>    <a href="login.jsp">返回登陆界面</a></body></html>

这里写图片描述
这里写图片描述
这里写图片描述

0 0
原创粉丝点击