req.setCharacterEncoding("UTF-8");
String name = req.getParameter("username");
String passwd = req.getParameter("passWd");
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();
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");
}
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
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/justleavel/article/details/121776621
内容来源于网络,如有侵权,请联系作者删除!