一个完整的简单jsp+servlet实例,实现简单的登录

来源:互联网 发布:淘宝8天货物没到货款 编辑:程序博客网 时间:2024/04/29 16:29
开发环境myeclipse+tomcat8

1、先创建web project,项目名为RegisterSystem,

2、在WebRoot 目录下创建login.jsp文件:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'login.jsp' starting page</title>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!-- 
  20.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  21.     -->  
  22.   
  23.   </head>  
  24.     
  25.   <body>  
  26.     This is my JSP page. <br>  
  27.      <form action="login">  
  28.     username:<input type="text" name="username"><br>  
  29.     password:<input type="password" name="pwd"><br>  
  30.     <input type="submit">  
  31.     </form>  
  32.   </body>  
  33. </html>  


3、在scr目录下的com.ht.servlet编写AccountBean.Java文件,代码如下:

package com.ht.servlet;

public class AccountBean {
 private String username;
 private String password;
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
 public String getUsername() {
  return username;
 }
 public void setUsername(String username) {
  this.username = username;
 }
}

4、在scr目录下的com.ht.servlet编写servlet类CheckAccount.java文件,代码如下:

package com.ht.servlet;

import java.io.IOException;

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

public class CheckAccount extends HttpServlet {

 @Override
 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {
  doGet(req,resp);
 }

 @Override
 public void doGet(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {
  HttpSession session = req.getSession();
  AccountBean account = new AccountBean();
  String username = req.getParameter("username");
  String pwd = req.getParameter("pwd");
  account.setPassword(pwd);
  account.setUsername(username);
  if((username != null)&&(username.trim().equals("jsp"))) {
   if((pwd != null)&&(pwd.trim().equals("1"))) {
    System.out.println("success");
    session.setAttribute("account", account);
    String login_suc = "success.jsp";
    resp.sendRedirect(login_suc);
    return;
   }
  }
  String login_fail = "fail.jsp";
  resp.sendRedirect(login_fail);
  return;
 }
 
}
5、在WebRoot目录下编写success.jsp文件 成功后跳转

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@page import="com.ht.servlet.AccountBean"%>      
  3. <%  
  4. String path = request.getContextPath();  
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  6. %>  
  7.   
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  9. <html>  
  10.   <head>  
  11.     <base href="<%=basePath%>">  
  12.       
  13.     <title>My JSP 'success.jsp' starting page</title>  
  14.       
  15.     <meta http-equiv="pragma" content="no-cache">  
  16.     <meta http-equiv="cache-control" content="no-cache">  
  17.     <meta http-equiv="expires" content="0">      
  18.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  19.     <meta http-equiv="description" content="This is my page">  
  20.     <!-- 
  21.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  22.     -->  
  23.   
  24.   </head>  
  25.     
  26.   <body>  
  27.       
  28.     <%  
  29.     AccountBean account = (AccountBean)session.getAttribute("account");  
  30.     %>  
  31.     username:<%= account.getUsername()%>  
  32.      <br>  
  33.      password:<%= account.getPassword() %>  
  34.        
  35.      basePath: <%=basePath%>  
  36.     path:<%=path%>  
  37.   </body>  
  38. </html>  

6、在WebRoot目录下编写fail.jsp文件 失败后跳转

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'fail.jsp' starting page</title>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!-- 
  20.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  21.     -->  
  22.   
  23.   </head>  
  24.     
  25.   <body>  
  26.      Login Failed! <br>  
  27.     basePath: <%=basePath%>  
  28.     path:<%=path%>  
  29.   </body>  
  30. </html>  

7、修改web.xml配置文件

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.   <welcome-file-list>  
  8.     <welcome-file>login.jsp</welcome-file>  
  9.   </welcome-file-list>  
  10.     
  11.   <servlet>  
  12.        <description>This is the description of my J2EE component</description>  
  13.        <display-name>This is the display name of my J2EE component</display-name>  
  14.          <servlet-name>CheckAccount</servlet-name>  
  15.        <servlet-class>com.ht.servlet.CheckAccount</servlet-class>  
  16.      </servlet>  
  17.      <servlet-mapping>  
  18.        <servlet-name>CheckAccount</servlet-name>  
  19.        <url-pattern>/login</url-pattern>  
  20.      </servlet-mapping>  
  21. </web-app>  

输入测试路径:http://localhost:8080/RegisterSystem/login.jsp

注意:

.处理中文乱码问题

在Sertlet中加

response.setContentType("text/html;charset=utf-8")

在jsp页面中加

<%@ page language="java" import="java.util.*,java.NET.URLEncoder" pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

 

在servlet中

String username=request.getParameter("username");

username=new String(username.getBytes(“ISO-8859-1”),"UTF-8");


reference :http://blog.sina.com.cn/s/blog_5c5bc9070100z7wb.html

http://lelglin.iteye.com/blog/967503

http://zhidao.baidu.com/link?url=qg1AcsUjhkG4gHBv8GtHDjIQuwgtLto1_eyeinBx-b8TW9HAcYRR1zizL8vTDPTAjLwHPOdz7NcwJun-HyJXeK

0 0
原创粉丝点击