jsp简单登陆实现

1:简单的登陆界面绘制

<div class="login">
        <form action="doLogin.jsp" method="post">
            <tr>
                <td>username</td>
                <td><input type="text" name="username" value="">
                </td>
            </tr>
            <br>
            <tr>
                <td>password</td>
                <td><input type="password" name="password" value="">
                </td>
            </tr>
            <br>
            <tr>
                <td colspan=2 align="center"><input type="submit" value="login">
                </td>
            </tr>
        </form>
    </div>

2:处理业务逻辑的dologin.jsp

<%@ page language="java" import="java.util.*"
    contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

  String username="";
  String password="";
  username=request.getParameter("username");
  password=request.getParameter("password");
 if("admin".equals(username)&&"admin".equals(password))
 {
    session.setAttribute("loginusername", username);
   request.getRequestDispatcher("login_success.jsp").forward(request, response);
 //请求重定向,成功后跳到的是dologin.jsp页面,客户端行为,相当于两次请求
 }
 else
 {
  response.sendRedirect("login_failure.jsp");
  //请求转发,成功后跳转到login_failure.jsp页面,服务端行为,相当于一次请求
 }
%>

3。登录成功显示成功状态

 <%!
  String name;
   %>
  <%
   
   if(session.getAttribute("loginusername")!=null)
   {
    name=session.getAttribute("loginusername").toString();
   
   }
  
   %>

 

posted @ 2015-01-29 14:56  BigShui  阅读(321)  评论(0编辑  收藏  举报