Servlet学习笔记02

Posted 夜色架构师

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Servlet学习笔记02相关的知识,希望对你有一定的参考价值。

昨天那个小测试我们完成了注册界面,今天我们来完成一下登陆界面:

首先上结果图:


Servlet代码:

1:首先我们需要继承HttpServlet类,然后重写方法:doGet和doPost

2:然后就是常规的一开始的编码设置:

  req.setCharacterEncoding("UTF-8");

3:接下来我们定义两个String字符串用来接受前端传过来的值(也就是用户输入以后提交的)

        String name = req.getParameter("username");
        String passwd = req.getParameter("passWd");

4:接下来就是基本的JDBC操作:

   String url = "jdbc:mysql://localhost:3306/servletstudy";
   Class.forName("com.mysql.cj.jdbc.Driver");
   Connection comm = DriverManager.getConnection(url,"root","root");

由于这里是要查询,所以我们定义SQL语句这样写:

   String SQL = "SELECT * FROM user where(username=? and password =?)";

预编译sql,然后执行:

                PreparedStatement pstmt = comm.prepareStatement(SQL);
                pstmt.setString(1,name);
                pstmt.setString(2,passwd);
                ResultSet rs = pstmt.executeQuery();

然后就是使用一个循环与数据库里面的对象作比较:

   if(rs.next()).....

在判断语句里面写重定向:

  if(rs.next())
                    if(rs!=null)
                        rs.close();
                    
                    if(pstmt!=null)
                        pstmt.close();
                    
                    if(comm!=null)
                        comm.close();
                    
//                    dis = req.getRequestDispatcher("http://localhost:8080/javaWeb_war_exploded/hello.jsp");
//                    dis.forward(req,resp);
                    resp.sendRedirect("http://localhost:8080/hello.jsp");
                    System.out.println("hah");
                else
                    if(rs!=null)
                        rs.close();
                    
                    if(pstmt!=null)
                        pstmt.close();
                    
                    if(comm!=null)
                        comm.close();
                    
//                    dis = req.getRequestDispatcher("http://localhost:8080/javaWeb_war_exploded/fruit.jsp");
//                    dis.forward(req,resp);
                    resp.sendRedirect("http://localhost:8080/false.jsp");
                

全部Loginservlet代码:

package com.ftz.Demo.Servlet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;

/**
 * @author $范涛之
 * @Description
 * @create 2021-12-07 4:32
 */
public class Login extends HttpServlet 
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException 
        super.doGet(req, resp);
    

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException 

        req.setCharacterEncoding("UTF-8");
        String name = req.getParameter("username");
        String passwd = req.getParameter("passWd");
        RequestDispatcher dis =null;
        System.out.println(name+passwd);
        try 
            try 
                String url = "jdbc:mysql://localhost:3306/servletstudy";
                Class.forName("com.mysql.cj.jdbc.Driver");
                Connection comm = DriverManager.getConnection(url,"root","root");
                String SQL = "SELECT * FROM user where(username=? and password =?)";
                //结果集
                PreparedStatement pstmt = comm.prepareStatement(SQL);
                pstmt.setString(1,name);
                pstmt.setString(2,passwd);
                ResultSet rs = pstmt.executeQuery();
//                System.out.println(rs.getString(1));
//                System.out.println(rs.getString(2));
//                System.out.println(rs.getString(3));
                if(rs.next())
                    if(rs!=null)
                        rs.close();
                    
                    if(pstmt!=null)
                        pstmt.close();
                    
                    if(comm!=null)
                        comm.close();
                    
//                    dis = req.getRequestDispatcher("http://localhost:8080/javaWeb_war_exploded/hello.jsp");
//                    dis.forward(req,resp);
                    resp.sendRedirect("http://localhost:8080/hello.jsp");
                    System.out.println("hah");
                else
                    if(rs!=null)
                        rs.close();
                    
                    if(pstmt!=null)
                        pstmt.close();
                    
                    if(comm!=null)
                        comm.close();
                    
//                    dis = req.getRequestDispatcher("http://localhost:8080/javaWeb_war_exploded/fruit.jsp");
//                    dis.forward(req,resp);
                    resp.sendRedirect("http://localhost:8080/false.jsp");
                

             catch (ClassNotFoundException e) 
                e.printStackTrace();
            
         catch (Exception e) 
            e.printStackTrace();
        
    


q

以上是关于Servlet学习笔记02的主要内容,如果未能解决你的问题,请参考以下文章

学习笔记:python3,代码片段(2017)

Servlet学习笔记

JAVAWEB学习笔记13_servlet

JavaWeb学习笔记-05初识servlet

jsp/servlet学习笔记(核心编程)servlet部分

JSP九大内置对象与Servlet学习笔记[转]