Java EE之servlet实现用户登录
1.在连接数据库的JAVA类中添加查询功能:
在这之前有一个连接数据库的方法:
Connection conn=null;
PreparedStatement stat=null;
ResultSet rs=null; //用来存储结果集
public WndMembers search(String name,String pw) throws SQLException{
WndMembers member=new WndMembers();
init(); //初始化的方法用来实现数据库连接
String searchsql="select wnd_membersname,wnd_memberspw from wnd_members where wnd_membersname='"+name+"'and wnd_memberspw='"+pw+"' "; //定义一个查询数据库的SQL语句
stat=conn.prepareStatement(searchsql); //这句话非常关键,只有有了这句话才能在查询数据库之前,连接数据库成功
if(name!=""&&pw!=""){
try {
rs=stat.executeQuery(searchsql); //把查询的结果返回给rs结果集
} catch (SQLException ex) {
Logger.getLogger(MyConnection.class.getName()).log(Level.SEVERE, null, ex);
}
}
try {
member.setWndMembersname(rs.getString(name));
member.setWndMemberspw(rs.getString(pw)); //如果查询到数据库中存在该数据,则返回给这个数据库生成的实体类的set方法,用于给Servlet获取
if(rs!=null){rs.close();}
if(stat!=null){
stat.close();
}
return member;
} catch (SQLException ex) {
Logger.getLogger(MyConnection.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
2.新建一个Servlet用于登录并响应相应界面:
package weinidingServlet;
import java.io.IOException;
import java.io.PrintWriter;
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 java.sql.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import weinidingServlet.MyConnection;
import weinidingJavaData.WndMembers;
import weinidingJavaData.WndDesigners;
import weinidingJavaData.WndFirms; //需要自己引入的外部类
/**
*
* @author 春霏
*/
@WebServlet(name = "logupServlet", urlPatterns = {"/logupServlet"})
public class logupServlet extends HttpServlet {
public logupServlet(){
super();
}
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) //dopost方法用来接收前端用post方法传过来的参数
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String name=request.getParameter("username");
String password=request.getParameter("password");
String designers=request.getParameter("designer");
String firms=request.getParameter("firm");
String members=request.getParameter("member"); //获取到参数并赋值给变量
if(designers!=null){
WndDesigners designer=new WndDesigners();
designer.setWndDesignersname(name);
designer.setWndDesignerspw(password);
MyConnection my=new MyConnection();
my.init();
try {
my.search2(name, password);
} catch (SQLException ex) {
Logger.getLogger(logupServlet.class.getName()).log(Level.SEVERE, null, ex);
}
processRequest(request, response); //登陆成功后响应的界面
}
if(members!=null){
WndMembers member=new WndMembers();
member.setWndMembersname(name);
member.setWndMemberspw(password);
MyConnection my=new MyConnection();
my.init();
try {
my.search(name, password);
} catch (SQLException ex) {
Logger.getLogger(logupServlet.class.getName()).log(Level.SEVERE, null, ex);
}
processRequest(request, response);
}
processRequest(request, response);
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet logupServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>恭喜你登录成功!!!</h1>");
out.println("</body>");
out.println("</html>");
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}