-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathFirstServlet.java
58 lines (48 loc) · 1.75 KB
/
FirstServlet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author jacobjohn
*/
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
class Login {
public static boolean validate(String name, String pass) {
boolean status = false;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/JavaJDBC?autoReconnect=true&useSSL=false", "root", "MyNewPass");
PreparedStatement ps = con.prepareStatement("select * from userreg where name=? and pass=?");
ps.setString(1, name);
ps.setString(2, pass);
ResultSet rs = ps.executeQuery();
status = rs.next();
} catch (Exception e) {
System.out.println(e);
}
return status;
}
}
public class FirstServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String n = request.getParameter("username");
String p = request.getParameter("userpass");
if (Login.validate(n, p)) {
RequestDispatcher rd = request.getRequestDispatcher("servlet2");
rd.forward(request, response);
} else {
out.print("Sorry username or password error");
RequestDispatcher rd = request.getRequestDispatcher("index.html");
rd.include(request, response);
}
out.close();
}
}