import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class Login extends HttpServlet{ /**
 * 
 */
private static final long serialVersionUID = 6459543349790159326L; @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


String userName=req.getParameter("userName");
String userPwd=req.getParameter("userPassword");
int userPassword=Integer.parseInt(userPwd);

if(new Stu().choose(userName,userPassword)){
resp.sendRedirect("/my2/Show.jsp");
}
else{
resp.sendRedirect("/my2/Login.jsp?error="+userName+" "+userPassword);
}
} @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

doGet(req,resp);
}}
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;public class Stu {
 public boolean choose(String userName,int userPassword){
 ResultSet rs = null;
 PreparedStatement pstmt = null;
 Connection con=null;
 
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDrive");
} catch (ClassNotFoundException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}

try {
 con = DriverManager.getConnection(
"jdbc:microsoft:sqlserver://localhost:1433;databasename=Master","sa","");
pstmt=con.prepareStatement(
"select * from s where user1=? and password1=?");
pstmt.setString(1,userName);
pstmt.setInt(2,userPassword);
rs=pstmt.executeQuery();
if(rs.next()){
return true;
}else{
return false;
}



} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
return false;
}finally{
try {
rs.close();
pstmt.close();

} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
if(con!=null){
try {
con.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}

}

}
 public static void main(String[] args)
 {
 if (new Stu().choose("ding",12))
{
System.out.println("用户名and密码正确。。");
}else
{
System.out.println("错误的用户名or密码");
}
 }
}