JSP 05

<% @ page language = " java " contentType = " text / html; charset = utf-8 " 
    pageEncoding = " utf-8 " %> 
<! DOCTYPE html > 
< html > 
< head > 
    <% - 
        1 . Create login Interface, username, password, verification code, login button
         2. servlet generates verification code, saves in session, and clicks refresh
         3. Login button jumps to servlet judgment interface
         4. First determine whether the verification code is correct, then determine the user name and Password
         5. Log in correctly to enter the login success interface, incorrectly return to the login interface and clear the session verification code to prevent reuse
         6. Delete the session object and exit
     --%>
<meta charset="utf-8">
<title>用户登录</title>
<style type="text/css">
    span{
        text-decoration:underline;
        color:blue;
    }
    div{
    color: red;
    }
</style>
</head>
<script type="text/javascript">
    window.onload=function(){
        var i=0;
        var yzm=document.getElementById("yzm");
        yzm.onclick=function(){
            i++;
            yzm.src="/web_homework2/yzm?"+i;
            
        }
        var span=document.getElementById("span");
        span.onclick=function(){
            yzm.onclick();
            
        } 
    }
</script>
<body>
    <form action="/web_homework2/testServlet" method="post">
        <input type="text" id="username" name="username" placeholder="你确定不输入yxy?"><br>
        <input type="password" id="password" name="password" placeholder="你确定不输入123456?"><br>
        <input type="text" name="yzm_text"placeholder = "see images enter" > < br > 
        < IMG Alt = "This is a verification code" the src = "/ web_homework2 / YZM" ID = "YZM" > < span ID = "span" > hell ah ! For a </ span > < br > 
        < INPUT type = "Submit" value = "Login" > 
    </ form > 
    < div > <% = Request.getSession (). The getAttribute ( " yzm_error ":request.getSession().getAttribute("yzm_error")%></div>
    <div><%=request.getSession().getAttribute("user_error")==null?"":request.getSession().getAttribute("user_error")%></div>
    <%request.getSession().removeAttribute("yzm_error"); 
    request.getSession().removeAttribute("user_error"); %>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>欢迎登录</title>
</head>
<script type="text/javascript">
    window.onload=function(){
        var zx=document.getElementById("zx");
        zx.onclick=function(){
            alert ( " Are you going away? " );
            document.getElementById("a").href="/web_homework2/exit";
        }
    }
</script>
<body>
    <%
        String name=(String)request.getSession().getAttribute("name");
    %>
    <p>欢迎登录,<%=name %>你是最酷的</p><br>
    <a id="a" href="#"><input type="submit" value="注销" id="zx"></a>
</body>
</html>
package servlet;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class yzm
 */
@WebServlet("/yzm")
public class yzm extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public yzm() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //创建图片对象
        int width=100;
        int height=50;
        BufferedImage image = new BufferedImage (width, height, BufferedImage.TYPE_3BYTE_BGR);
         // Get the brush 
        Graphics g = image.getGraphics ();
         // Fill the background color 
        g.setColor (Color.PINK);
        g.fillRect ( 0, 0 , width, height);
         // Draw blue border 
        g.setColor (Color.BLUE);
        g.drawRect ( 0, 0, width-1, height-1 );
         // Generate random alphanumerics, write using loop, and save session 
        String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" ;
        Random r = new Random();
        g.setColor(Color.RED);//红色字
        StringBuilder sBuilder=new StringBuilder();
        for (int i = 0; i < 4; i++) {
            int index = r.nextInt(str.length());
            char ch = str.charAt(index);
            g.drawString(ch+"", width/5+i*20, height/2);//调整位置
            sBuilder.append(ch);
        }
        String yzm_session = sBuilder.toString();
        request.getSession().setAttribute("yzm_session", yzm_session);
        //画干扰线绿色
        g.setColor(Color.GREEN);
        for (int i = 0; i < 10; i++) {
            int x1 = r.nextInt(width);
            int x2 = r.nextInt(width);
            int y1 = r.nextInt(height);
            int y2 = r.nextInt(height);
            g.drawLine(x1, y1, x2, y2);
        }
        // The output image is displayed on the webpage 
        ImageIO.write (image, "jpg" , response.getOutputStream ());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class TestServlet
 */
@WebServlet("/testServlet")
public class TestServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TestServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("utf-8");
        HttpSession session = request.getSession();
        String yzm = (String)session.getAttribute("yzm_session");
        session.removeAttribute ( "yzm_session"); // Prevent verification code reuse 
        String yzm_text = request.getParameter ("yzm_text" );
         if (yzm_text.equalsIgnoreCase (yzm)) {
             // Verify code is correct, continue to judge username and password 
            String username = request.getParameter ("username" );
            String password = request.getParameter ("password" );
             if (username.equals ("yxy") && password.equals ("123456" )) {
                 // Authentication is successful, jump to login successful interface 
                request.getSession (). SetAttribute ("name" , username);
                request.getRequestDispatcher("/loginSuccess.jsp").forward(request,response);
            } else {
                 // Authentication fails, return to the login interface, and prompt error message 
                request.getSession (). setAttribute ("user_error", "Username or password is wrong!" );
                request.getRequestDispatcher("/login.jsp").forward(request, response);
            }
        } else {
             // Wrong verification code, return to the login interface, and prompt error message 
            request.getSession (). setAttribute ("yzm_error", "Verification code error!" );
            request.getRequestDispatcher("/login.jsp").forward(request, response);
        }
        
        
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class exit
 */
@WebServlet("/exit")
public class exit extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public exit() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.getSession().invalidate();//终结session
        request.getRequestDispatcher("/login.jsp").forward(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

 

Guess you like

Origin www.cnblogs.com/yanzhen108/p/12679220.html
jsp
05