import javax.servlet.RequestDispatcher; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Enumeration; public class LoginServlet extends HttpServlet { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 设置响应MIME类型 resp.setContentType("text/html;charset=utf-8"); // 获得输出流 PrintWriter out = resp.getWriter(); if (isRight(req, resp)) { // 获取客户端信息和服务器信息 String clientAddress = req.getRemoteAddr(); int clientPort = req.getRemotePort(); String clientHost = req.getRemoteHost(); String serverAddress = req.getLocalAddr(); int serverPort = req.getLocalPort(); String serverHost = req.getLocalName(); // 输出信息到客户浏览器 out.print("<html><head><title>"); out.print("登陆成功!</title></head><body><center>"); out.print("<p>客户机IP:" + clientAddress + " 客户机端口:" + clientPort + " 客户机主机名:" + clientHost); out.print("<p>服务器IP:" + serverAddress + " 服务器端口:" + serverPort + " 服务器主机名:" + serverHost); // 获取http请求信息 Enumeration headNames = req.getHeaderNames(); out.print("<p>http请求信息<br><table border=\"1\">"); while (headNames.hasMoreElements()) { String headName = (String) headNames.nextElement(); String value = req.getHeader(headName); out.print("<tr><td>" + headName + "</td><td>" + value + "</td></tr>"); } out.print("</table>"); // 获得上下文,一个上下文代码一个web应用 ServletContext context = getServletContext(); // 统计访问量,context.getAttribute("count");这个Attribute是属于整个web应用程序的 // 该应用程序的每个servlet都可以访问此属性 Integer count = (Integer) context.getAttribute("count"); if (count == null) count = 1; else count++; context.setAttribute("count", count); out.print("<p><br>当前访问量:" + count); out.print("</body></html>"); } else { // 请求转发 // RequestDispatcher由servlet容器所创建,用于封装一个由路径所标识的资源 // 1.参数表示相对于当前servlet路径,也可以是上下文路径 // RequestDispatcher rd=req.getRequestDispatcher("success.html"); // 2.参数是相对上下文路径,必须要以/开始 //RequestDispatcher rd = getServletContext().getRequestDispatcher("/success.html"); // 控制权交给转发到的servlet,out输出的内容将会被清除,其他后面的代码也不会执行 //rd.forward(req, resp); // 控制权交给转发到的servlet,执行完后返回当前servlet并继续执行 // rd.include(req, resp); // 响应发送到指定页面,和rd.forward(req, resp);有区别 resp.sendRedirect("login.html"); } out.close(); } // 通过查询数据库,判断输入的用户名密码是否正确 private boolean isRight(HttpServletRequest req, HttpServletResponse resp) { // 获取表单数据 String user = req.getParameter("user"); String password = req.getParameter("password"); // 获取初始参数,下面是获得连接数据库的参数 String driverClass = getInitParameter("driverClass"); String url = getInitParameter("url"); String username = getInitParameter("username"); String pw = getInitParameter("password"); DataBaseConnection dbConnection = new DataBaseConnection(); Statement st = dbConnection.getStatement(driverClass, url, username, pw); try { ResultSet result = st.executeQuery("select * from user"); while (result.next()) { if (result.getString("username").equals(user) && result.getString("password").equals(password)) return true; } result.close(); st.close(); } catch (SQLException e) { e.printStackTrace(); } dbConnection.close(); return false; } }
简单Java Web应用程序搭建与部署(2)
内容版权声明:除非注明,否则皆为本站原创文章。