标签:
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class InfoServlet
*/
@WebServlet("/InfoServlet")
public class InfoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
UserInfo userInfo = new UserInfo();
private Connection conn;
private ResultSet rs;
private PreparedStatement pst = null;
/**
* @see HttpServlet#HttpServlet()
*/
public InfoServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置响应内容的字符集编码
request.setCharacterEncoding("utf-8");
//获取表单数据
String name = request.getParameter("name");
String password = request.getParameter("password");
// TODO Auto-generated method stub
//设置响应内容类型
response.setContentType("text/html");
//设置响应内容的字符集编码
response.setCharacterEncoding("utf-8");
//获取响应内容输出流
PrintWriter out = response.getWriter();
//连接数据库
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/xx","root","root");
String str = "SELECT id from text where name= ? and password = ?";
pst = conn.prepareStatement(str);
pst.setString(1, name);
pst.setString(2, password);
rs = pst.executeQuery();
while(rs.next()){
userInfo.setId(rs.getInt("id"));
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
System.out.println(rs.getInt("id"));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(name);
System.out.println(password);
System.out.println(userInfo.getId());
if(userInfo.getId() !=0){
response.sendRedirect("http://localhost:8080/Myhome/YesServlet");
}else{
response.sendRedirect("http://localhost:8080/Myhome/NoServlet");
}
out.close();
}
}
标签:
原文地址:http://www.cnblogs.com/sdswl/p/5801247.html