关闭

单java+2jsp页面实现登录验证

418人阅读 评论(0) 收藏 举报

////////////////////Check.java
import java.io.IOException;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;




public class Check extends HttpServlet{


private Map<String, String> usermap;

public Check()
{
usermap=new HashMap<String, String>();
usermap.put("caterpillar","Hello");
usermap.put("Justin","welcome");
usermap.put("momor", "Hi");
usermap.put("宏", "123");
usermap.put("hong", "456");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response )throws ServletException, IOException
{
request.setCharacterEncoding("UTF-8");
String name=request.getParameter("user");
String password=request.getParameter("password");

if(   password.equals(usermap.get(name))    )
{
//response.sendRedirect("jieguo.jsp");


request.setAttribute("message", "logining.....");
request.getRequestDispatcher("jieguo.jsp").forward(request, response);
}
else
{
//request.setAttribute("user", name);
request.setAttribute("message", "sorry 登陆失败");
request.getRequestDispatcher("jieguo.jsp").forward(request, response);
}




//request.getRequestDispatcher("login.jsp").forward(request, response);
}

}
///////////////////////////////////////////////////////////////////////////////////////////////////
//////////login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'login.jsp' starting page</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->


  </head>
  
  <body> 
  <form action="Check.do" method="post">
  用户名<input type="text" name="user"><br>&nbsp; 
  密码<input type="password" name="password"><br>
  <input type="submit" value="登陆">
  
  </form>
    <br>
  </body>
</html>


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


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'jieguo.jsp' starting page</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->


  </head>
  
  <body> 
    ${param.user}&nbsp;&nbsp;  ${message} <br>
  </body>
</html>

////////////////////////////////////web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<servlet>



<servlet-name>Check</servlet-name>
<servlet-class>Check</servlet-class>
</servlet>


<servlet-mapping>
<servlet-name>Check</servlet-name>
<url-pattern>/Check.do</url-pattern>
</servlet-mapping>

  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
</web-app>


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:173496次
    • 积分:3238
    • 等级:
    • 排名:第10559名
    • 原创:143篇
    • 转载:62篇
    • 译文:1篇
    • 评论:36条
    最新评论