This article illustrates how JSP uses the servlet as a controller to implement the MVC pattern. Share to everyone for your reference. Specifically as follows:
First, the goal:
① a preliminary understanding of the MVC pattern;
② Master the writing of the servlet;
③ uses the MVC pattern to complete the login function.
Second, the main content:
① analyzes the problems existing in the Jsp+javabean model, and introduces the Jsp+javabean+servlet mode, and the relationship with the MVC pattern;
② introduces the writing, configuration and operation of the servlet through a simple example;
③ with the servlet to complete the login function control.
1, Jsp+javabean The problem of this mode?
The powerful JSP is in the interaction with people, that is, to complete the function of input and output. But in Jsp+javabean this mode, the JSP not only completes the function of input and output, but also completes the control function of the system (receives the user request, invokes the JavaBean, and then according to the result of the call, chooses the interface to respond to the user). So in the third phase of JSP development, the control function is separated from the JSP, use the servlet to realize, form jsp+javabean+servlet this pattern, JSP only completes the input and the output, JavaBean completes the processing, but the servlet completes the control.
2, Jsp+javabean+servlet
JSP is responsible for input and output, JavaBean is responsible for implementing business logic (function), servlet complete control. This pattern is often also considered an implementation of the MVC pattern. The MVC pattern separates the business logic, control and input output of the system, and when developing the application, it can consider a certain part and simplify the development.
V, which represents the view, the section with the human settlement, M, the representation model, completion function, C, represents the controller. A JSP usually acts as a view, JavaBean is a model, and a servlet acts as a controller.
3, what is a servlet
A servlet is also a Web component that is the same as a JSP from the functionality it completes. The servlet is a pure Java file and is a special Java class.
What is the difference between a servlet and a JavaBean? are Java classes, but Serlvet can receive requests from users, and clients can access them directly. However, JavaBean cannot be accessed directly by the client and must be invoked by JSP or other Java files (including servlet).
4. Example: HelloServlet
Below is a servlet that completes the function of outputting a word "servlet test!" "。
1) Document Preparation
Package servlet;
Servlet development required for the package
import java.io.*;
Import javax.servlet.*;
Import javax.servlet.http.*;
public class HelloServlet extends HttpServlet
{public
void doget (HttpServletRequest request, HttpServletResponse response)
throw ioexception,servletexception
{
response.setcontenttype ("text/ html;charset=gb2312 ");
PrintWriter out = Response.getwriter ();
Out.print ("servlet test!") ");
}
}
2) compiling
You need to configure the C:/Program Files/apache Software foundation/tomcat 6.0/lib/servlet-api.jar to Classpath, and then compile.
3) configuration
Write in the configuration file Web.xml. Includes two parts: Declaration and access mode settings.
Statement:
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>servlet. Helloservlet</servlet-class>
</serlvet>
Access Mode settings:
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</ Url-pattern>
</servlet-mapping>
5, testing
Visit: Http://127.0.0.1:8080/ch6/hello
6. How to use this mode to implement login function?
Interface and response interface do not need to be modified, JavaBean finish processing, do not need to change, just use the servlet to replace the original login-process.jsp, complete the control function.
1) The reference code is as follows:
Package servlet;
Import java.io.*;
Import javax.servlet.*;
Import javax.servlet.http.*;
Import javabean.*;
public class Loginprocess extends HttpServlet
{public
void doget (HttpServletRequest request, HttpServletResponse response)
throws Ioexception,servletexception
{
//First step: Get user input information
String Username = Request.getparameter ("username");
String Userpass = Request.getparameter ("Userpass");
Step two: Invoke JavaBean
User user = new user ();
User.setusername (username);
User.setuserpass (userpass);
Boolean B = User.check ();
Step three: Select an interface to respond to the user
String forward;
if (b)
forward = "success.jsp";
else
forward = "failure.jsp";
RequestDispatcher rd = Request.getrequestdispatcher (forward);
Rd.forward (Request,response);
}
The code above basically shows the basic functionality of a servlet as a controller.
2) configuration
<servlet>
<servlet-name>process</servlet-name>
<servlet-class>servlet. loginprocess</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> process</servlet-name>
<url-pattern>/process</url-pattern>
</servlet-mapping>
3 Modify the Login Interface action attribute
<%@ page contenttype= "text/html;charset=gb2312"%>
Please login <br> <form name= "Form1"
post "action=" process >
user id:<input type= "text" name= "username" ><br>
password: <input type= " Password "name=" Userpass "><br>
<input type=" Submit "value=" Login "><input type=" reset "value=" reset ">
</form>
<%@ include file=" contact.jsp "%>
7. Test Run
Access the Login interface, enter the information, and then submit.
At this time will be an error, prompt request method does not support. The following methods need to be added to the servlet:
public void DoPost (httpservletrequest request,httpservletresponse response)
throws Ioexception,servletexception
{
doget (request,response);
}
Because the request in the JSP page is post, the servlet needs to provide the Dopost method with the same definition as doget, where the implementation of the method requires only a call to the Doget method.
8, the main method of the servlet
Init method, for initialization;
Service class Methods: Doget method and Dopost method
Destroy method, releasing resources
9. Life cycle
After the server receives the request, it encapsulates the request information into the HttpServletRequest object and the HttpServletResponse object.
The first time you access the servlet, load the class, create the object, Initialize (init method), invoke the service class method (if it is a GET request, call the Doget method, and call the Dopost method if it is a POST request).
Subsequent access, call the server class method directly.
When you uninstall the servlet, or when you shut down the server, call the Destroy method to release the resource.
I hope this article will help you with the JSP program design.