• 登录实现之servlet和SpringMVC


    #知识小点

     

    防止乱码的字符集的设置:

            request.setCharacterEncoding():确定从请求端发送到给服务器的编码是汉字字符集。该方法对get方法

    无效,只对post方法有效。若要对get方法有效,则要在server.xml指定。

     

          response.setCharacterEncoding():用来服务器给客户端的编码。或者设置response.setContentType()

    方法来设置Http响应的编码。

     

    说明:一般默认是按照iso8859-1解码,这个只支持英文,不支持中文,所以一般支持中文要改为utf-8。

     ----------------------------------------------------------------------------------

     

    转发和重定向:转发,只能在当前项目工程或者站点转发,一次请求和一次响应,重定向是两次请求,耗时相对

    较长,所以一般既能用转发,又能够用重定向的话,就用重定向,但是如果要跳转到其它站点,比如百度,则不

    能用转发,只能用重定向。

     

     ----------------------------------------------------------------------------------  

    get和post使用场景: ...

    参看 https://blog.csdn.net/shangrila_kun/article/details/83658646

     

     -----------------------------------------------------------------------------------

    #登录程序方案一

        servlet(session和转发、重定向)

    LoginServlet.java
     1 package web;
     2 
     3 import javax.servlet.ServletException;
     4 import javax.servlet.http.HttpServlet;
     5 import javax.servlet.http.HttpServletRequest;
     6 import javax.servlet.http.HttpServletResponse;
     7 import javax.servlet.http.HttpSession;
     8 import java.io.IOException;
     9 /**
    10  * s1:设置请求端字符编码为utf-8
    11  * s2:从前端页面获取请求参数
    12  * s3:如果"king".equals(uname) && "test".equals(pwd)为true
    13  *    则表示之前登录过的,则获取其session对象(getSession方法),并绑定它的值,
    14  *    并重定向到welcom.jsp,  否则,在请求端绑定值并给出错误提示,
    15  *    并转发到"login.jsp";
    16  * */
    17 public class LoginServlet extends HttpServlet {
    18 
    19         @Override
    20         protected void service(
    21                 HttpServletRequest request,
    22                 HttpServletResponse response)
    23                     throws ServletException,
    24                 IOException {
    25             request.setCharacterEncoding("utf-8");
    26 
    27             //读取用户名和密码
    28             String uname = request.getParameter("uname");
    29             String pwd = request.getParameter("pwd");
    30 
    31 
    32             if ("king".equals(uname) && "test".equals(pwd)) {
    //说明之前登录了,则获取和绑定session对象,并重定向到welcome.jsp
    33 HttpSession session = request.getSession(); 34 session.setAttribute("user", uname); 35 response.sendRedirect("welcome.jsp"); //重定向要保持同一站点 36 } else { 37 //request.setAttribute();它与转发联用的,并且能用于页面传值 38 request.setAttribute("login_failed","用户名或密码错误!"); 39 request.getRequestDispatcher("login.jsp").forward(request,response); 40 41 } 42 } 43 }

    welcome.jsp

     1 <%
     2     Object obj = session.getAttribute("user");
     3     if(obj == null){
     4         response.sendRedirect("login.jsp");
     5         return;
     6     }
     7 %>
     8 <%@ page pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
     9 <html>
    10 <head>
    11 
    12 </head>
    13 <body style="font-size:30px;">
    14 <h1 align="center" style="color:red;">恭喜你,登录成功!</h1>
    15 <%
    16     System.out.println("登录成功...");
    17 %>
    18 </body>
    19 </ht

    login.jsp

     1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
     2 <html>
     3 <head>
     4     <title>登录</title>
     5 </head>
     6 <body>
     7 <form action="login" method="post">
     8     <fieldset>
     9         <legend>登录</legend>
    10         用户名:<input name="uname"/>
    11         <%
    12             String msg =(String)request.getAttribute("login_failed");
    13         %>
    14                 <%=msg == null ? "" : msg%>
    15         <br/>
    16         密码:<input type="password" name="pwd"/><br/>
    17         <input type="submit" value="确定"/>
    18     </fieldset>
    19 </form>
    20 </body>
    21 
    22 </body>
    23 </html>

    web.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
     3 
     4   <servlet>
     5     <servlet-name>loginServlet</servlet-name>
     6     <servlet-class>web.LoginServlet</servlet-class>
     7   </servlet>
     8   <servlet-mapping>
     9     <servlet-name>loginServlet</servlet-name>
    10     <url-pattern>/login</url-pattern>
    11   </servlet-mapping>
    12 
    13   <servlet>
    14     <servlet-name>someServlet</servlet-name>
    15     <servlet-class>web.SomeServlet</servlet-class>
    16   </servlet>
    17   <servlet-mapping>
    18     <servlet-name>someServlet</servlet-name>
    19     <url-pattern>/some</url-pattern>
    20   </servlet-mapping>
    21 
    22 </web-app>
    成年人的世界没有那么多的童话,也没有那么多的逆袭。
  • 相关阅读:
    LateX安装记录
    阅读《基于区块链技术的溯源系统 》总结(硕士论文)
    阅读《轻量级比特币交易溯源机制》总结
    论文复现实践
    20199316 2019-2020-2 《网络攻防实践》第12周作业
    20199316 2019-2020-2 《网络攻防实践》第十一周作业
    网络空间安全学习笔记
    20199316 2019-2020-2 《网络攻防实践》第10周作业
    20199316 2019-2020-2 《网络攻防实践》第9周作业
    20199316 2019-2020-2 《网络攻防实践》第8周作业
  • 原文地址:https://www.cnblogs.com/shijinglu2018/p/10351798.html
Copyright © 2020-2023  润新知