package test;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
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;
public class UserLogin extends HttpServlet{
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
request.setCharacterEncoding("GBK");
response.setContentType("text/html;charset=GBK");
String name = request.getParameter("name");
String password = request.getParameter("password");
String loginName="";
String loginPassword="";
Connection conn = null;
PreparedStatement ps = null;
try {
//注册驱动
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
//获取连接
conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.*:*:*",username","password");
//数据库操作语句
String sql = new StringBuffer()
.append("select * from user ")
.toString();
ps = conn.prepareStatement(sql);
//获取状态集
ResultSet rs = ps.executeQuery();
//rs映射
while(rs.next()){
loginName= rs.getString(1);
loginPassword= rs.getString(2);
}
if(loginName.equals(name) && loginPassword.equals(password)){
request.setAttribute("username", loginName);
request.getRequestDispatcher("/pay/success.jsp").forward(request, response);
return;
}else{
request.setAttribute("msg", "用户名或密码错误");
request.getRequestDispatcher("/pay/login.jsp").forward(request, response);
// response.sendRedirect("http://www.baidu.com");
}
} catch (Exception e) {
e.printStackTrace();
} finally{
//关闭流
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(ps!=null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
登录注册简单servlet案列
最新推荐文章于 2022-10-31 16:49:10 发布