📄 validatorservlet.java
字号:
package org.chooseClass.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.chooseClass.database.DBConnect;
public class ValidatorServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public ValidatorServlet() {
super();
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username").trim();
String password = request.getParameter("password").trim();
String type = request.getParameter("type");
ResultSet rs=null;
try {
if(type.equalsIgnoreCase("student")){
//java.lang.Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
//String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=ChooseClass";
//conn=DriverManager.getConnection(url,"sa","sa");
String sql = "select * from student where 学号=? and 密码=?";
DBConnect dbc=new DBConnect(sql);
dbc.setString(1, username);
dbc.setString(2, password);
rs=dbc.executeQuery();
//Statement stmt = conn.createStatement();
//rs=stmt.executeQuery(sql);
}
else if(type.equalsIgnoreCase("teacher"))
{
DBConnect dbc=new DBConnect("select * from teacher where 教师编号=? and 密码=?");
dbc.setString(1, username);
dbc.setString(2, password);
rs=dbc.executeQuery();
}else if(type.equalsIgnoreCase("admin"))
{
DBConnect dbc=new DBConnect("select * from login where 用户名=? and 密码=?");
dbc.setString(1, username);
dbc.setString(2, password);
rs=dbc.executeQuery();
}
if(rs.next()){
request.getSession().setAttribute("username", username);
request.getSession().setAttribute("type",type);
if(type.equalsIgnoreCase("student")){
RequestDispatcher rd = getServletContext().getRequestDispatcher("/student/student.jsp");
rd.forward(request,response);
return;
}else
if(type.equalsIgnoreCase("teacher")){
RequestDispatcher rd = getServletContext().getRequestDispatcher("/teacher/teacher.jsp");
rd.forward(request,response);
return;
}else
if(type.equalsIgnoreCase("admin")){
RequestDispatcher rd = getServletContext().getRequestDispatcher("/admin/admin.jsp");
rd.forward(request,response);
return;
}
}else{
RequestDispatcher rd = getServletContext().getRequestDispatcher("/login.jsp?errmsg=用记名或密码错误!");
rd.forward(request,response);
return;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -