登录页面代码
package org.zjh.modek;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.zjh.util.DButil;
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
String userName = request.getParameter("userName");
String pwd = request.getParameter("pwd");
boolean isTrue = false;
Connection conn = DButil.getconn();
String sql = "select * from users where user_name = ? and pwd = ? ";
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
ps.setString(1, userName);
ps.setString(2, pwd);
rs = ps.executeQuery();
if (rs.next()) {
isTrue = true;
} else {
isTrue = false;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DButil.close(conn, ps, rs);
PrintWriter out = response.getWriter();
if (isTrue) {
//跳转用户列表
response.sendRedirect("user.jsp");
} else {
//跳转注册页面
response.sendRedirect("index.jsp");
}
out.flush();
out.close();
}
}
登录jsp代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"