登录页面就不写了logOn.jsp提交给LoginServlet.java
LoginServlet.java代码:
package com.gsi.role.servlet;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.gsi.role.db.ConnectionPool;
import com.gsi.role.db.DB;
/**
* Servlet implementation class for Servlet: LoginServlet
*/
public class LoginServlet extends javax.servlet.http.HttpServlet implements
javax.servlet.Servlet {
/*
* (non-Java-doc)
*
* @see javax.servlet.http.HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
}
/*
* (non-Java-doc)
*
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
* HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/*
* (non-Java-doc)
*
* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
* HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType( "text/html ");
String result = " ";
// 获取提交的用户名:
String strUserName = request.getParameter( "C_Usr_Id ");
// 获取提交的密码:
String strPassword = request.getParameter( "C_Usr_Passwd ");
if ((strUserName == null) || (strUserName.equals( " "))
|| (strUserName.length() > 20)) {
result = "请输入用户名(不超过20字符) ";
request.setAttribute( "error_username ", result);
response.sendRedirect( "logOn.jsp ");
}
if ((strPassword == null) || (strPassword.equals( " "))
|| (strPassword.length() > 20)) {
result = "请输入密码(不超过20字符) ";
request.setAttribute( "error_password ", result);
response.sendRedirect( "logOn.jsp ");
}
// 创建数据库连接对象:
ConnectionPool connPool = DB.getConnPool();
Connection conn = connPool.getConnection();
Statement stmt = null;
ResultSet rs = null;
String sql= " ";
try {
// sql语句:
sql = "select * from sys_usr where C_Usr_Id= ' "+ strUserName + " ' and C_Usr_Passwd= ' " + strPassword + " ' ";