JAvaWebによるログイン機能の実現

5110 ワード

1.三要素
(1)入り口が私たちのページです
処理に入力データ要求に文字化けし、requestを使用する.SetCharacterEncoding("UTF-8");解決するには、Postリクエストにのみ使用されます.
(2)処理はサーブレットで実行されるものである.
出口まで処理する間も文字化けしてresponse.SetCharacterEncoding(「UTF-8」)で解決
(3)出口は私たちがジャンプするページです
 
  web.xml構成は次のとおりです.


  
    LoginServlet
    com.beiwo.servlet.LoginServlet
  

  
    LoginServlet
    /LoginServlet
  
  
    index.jsp
  


 
2.まずログイン画面を書く


  
    Login.html
	
    
    
    
    
    

  
  
  
   
   


// , ログイン

 3.ログインをクリックするとservletに入ります
package com.beiwo.servlet;

import java.io.IOException;
import java.io.PrintWriter;

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

public class LoginServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public LoginServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. 
*/ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet.
* * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println(" doGet "); String name = request.getParameter("username"); String password = request.getParameter("pwd"); System.out.println(" :" + name + " :" + password); request.setAttribute("username", name); request.setAttribute("pwd", password); // request.getRequestDispatcher("Success.jsp").forward(request, response);
               //html , jsp          //
    response.sendRedirect("Success.jsp"); } /** * The doPost method of the servlet.
* * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println(" doPost ");
          // String name = request.getParameter("username"); String password = request.getParameter("pwd"); System.out.println(" :" + name + " :" + password);
          // request.setAttribute("username", name); request.setAttribute("pwd", password); // request.getRequestDispatcher("Success.jsp").forward(request, response);
          //
              //response.sendRedirect("Success.jsp"); } /** * Initialization of the servlet.
* * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } }

 4.別のページに移動し、ユーザー名とパスワードを取得します.

// utf-8,
Success.html // ${} :${username} :${pwd}

 
転載先:https://www.cnblogs.com/houjiie/p/6203287.html