Demo_JSP_JavaBean_Servlet of architecture instance, jspservletjavabean
Demo_JSP_JavaBean_Servlet for schema instances
1,Development tools and development environment
Development tools: MyEclipse10, JDK1.6.0 _ 13 (32-bit), Tomcat7.0 (32-bit), and mysql5.7.13
Development Environment: WIN10
2,Demo_JSP_JavaBean_Servlet implementation function
User Logon, user registration, and logout.
3,Demo_JSP_Java_Bean_Servlet Usage Technology
This example uses JSP, JavaBean, Servlet, and JDBC to Implement User Logon, user registration, and login logout. Figure 1 shows the system architecture:
Figure 1: Demo_JSP_Java_Bean_Servlet System Architecture
See figure 2 (logical relationship between JSP, JavaBean, and Servlet in the system ):
Figure 2: Logical Relationship Between JSP, JavaBean, and Servlet IN THE SYSTEM
4,Implementation
The sample code of this blog is modified based on the Code of my previous blog. The modified part is to add a Servlet and delete the files in the JSP file that process the logic layer, the following describes the modified part-Servlet:
First, create a Servlet class in the project Demo_JSP_Java_Bean_Servlet: the class name is UserServlet, and the package name is liu_servlet. this Servlet class mainly implements logic processing for user login, registration, and exit, and call JavaBean to operate the database. The specific code of the UserServlet class is as follows:
package liu_servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import liu.UserBean;public class UserServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; /** * Constructor of the object. */ public UserServlet() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String method = (String)request.getParameter("method"); if(method==null) { PrintWriter out = response.getWriter(); out.println("invalid request!"); } else if(method.equals("login")) { Login(request, response); } else if(method.equals("logout")) { Logout(request, response); } else if(method.equals("register")) { Register(request, response); } } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ protected void Login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // get parameters String username = request.getParameter("username"); String password = request.getParameter("password"); // check null if (username == null || password == null) { response.sendRedirect("login.jsp"); return; } // validate UserBean userBean = new UserBean(); boolean isValid = userBean.valid(username, password); if (isValid) { HttpSession session = request.getSession(); session.setAttribute("username", username); response.sendRedirect("welcome.jsp"); return; } else { response.sendRedirect("login.jsp"); return; } } protected void Logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); session.removeAttribute("username"); response.sendRedirect("login.jsp"); } protected void Register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // get parameters String username = request.getParameter("username"); String password1 = request.getParameter("password1"); String password2 = request.getParameter("password2"); String email = request.getParameter("email"); // check null if (username == null || password1 == null || password2 == null || !password1.equals(password2)) { response.sendRedirect("register.jsp"); return; } // validate UserBean userBean = new UserBean(); boolean isExist = userBean.isExist(username); if(!isExist) { userBean.add(username, password1, email); response.sendRedirect("login.jsp"); return; } else { response.sendRedirect("register.jsp"); return; } } public void init() throws ServletException { // Put your code here }}
Next, view the web. xml file under the Demo_JSP_Java_Bean_Servlet project. The code of my web. xml file is as follows:
<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>UserServlet</servlet-name> <servlet-class>liu_servlet.UserServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>UserServlet</servlet-name> <url-pattern>/UserServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
Note: <url-pattern>/UserServlet </url-pattern> is modified after being opened. The code before modification should be: <url-pattern>/servlet/UserServlet </url-pattern>. If you do not remove the/servlet, when you perform logic processing to jump to the interface, it will appear in the URL with more... /servlet/welcome. jsp to report that there is no welcome in Tomcat. jsp file compilation error.
For other part of the code and Result Display, please refer to my previous blog: http://www.cnblogs.com/liuzhen1995/p/5700789.html
Attachment: Demo_JSP_Java_Bean_Servlet project source code file Baidu cloud download link: http://pan.baidu.com/s/1geEOyht password: zhu4