1. 程式人生 > >一個完整的簡單jsp+servlet例項,實現簡單的登入

一個完整的簡單jsp+servlet例項,實現簡單的登入

開發環境myeclipse+tomcat8

1、先建立web project,專案名為RegisterSystem,

2、在WebRoot 目錄下建立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>
    This is my JSP page. <br>
     <form action="login">
    username:<input type="text" name="username"><br>
    password:<input type="password" name="pwd"><br>
    <input type="submit">
    </form>
  </body>
</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檔案 成功後跳轉

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="com.ht.servlet.AccountBean"%>    
<%
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 'success.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>
    
    <%
    AccountBean account = (AccountBean)session.getAttribute("account");
    %>
    username:<%= account.getUsername()%>
     <br>
     password:<%= account.getPassword() %>
     
     basePath: <%=basePath%>
    path:<%=path%>
  </body>
</html>

6、在WebRoot目錄下編寫fail.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 'fail.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>
     Login Failed! <br>
    basePath: <%=basePath%>
    path:<%=path%>
  </body>
</html>

7、修改web.xml配置檔案

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
       <description>This is the description of my J2EE component</description>
       <display-name>This is the display name of my J2EE component</display-name>
         <servlet-name>CheckAccount</servlet-name>
       <servlet-class>com.ht.servlet.CheckAccount</servlet-class>
     </servlet>
     <servlet-mapping>
       <servlet-name>CheckAccount</servlet-name>
       <url-pattern>/login</url-pattern>
     </servlet-mapping>
</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