The development of Jsp+javabean two-storey structure should be more familiar and better understood.
But one thing must be clear is the user through the browser to send a Web page request, the request to the server after the server to find the corresponding page, if it is the first request (the second do not have to explain the implementation), for JSP to generate a servlet, The servlet engine is then used to execute the servlet, embedding the results of the call JavaBean into the browser that is returned to the user in the page.
The essence of the Jsp+javabean+servlet three-tier structure is a controller:servlet to distribute the client browser's request. It would be helpful to understand the servlet if the role of the servlet acting as a controller is understood as preprocessing of the client's request. The Web.xml profile allows you to find the corresponding relationship between a user request and a particular servlet, each of which has a specific servlet object, so that the processing user requests is a Servlet object that inherits from HttpServlet.
<!--jspc servlet mappings start
<servlet>
<servlet-name>ms1</servlet-name& Gt
<servlet-class>news. Firstaction</servlet-class>
</servlet>
<servlet>
<servlet-name>ms2</ Servlet-name>
<servlet-class>news. Detailaction</servlet-class>
</servlet>
<!--jspc servlet mappings end;
< Servlet-mapping>
<servlet-name>ms1</servlet-name>
<url-pattern>/newsmain</ Url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ms2</ Servlet-name>
<url-pattern>/newsdetail</url-pattern>
</servlet-mapping>
As shown above, a section of the configuration servlet from Web.xml, the first part is primarily used to configure the servlet to be associated with a specific Servlet object, and the second is primarily used to configure which servlet handles the request, the Servlet name Association, and the processing request associated with the specific Servlet processing object , for example, the client browser sends a/NEWSMAIN request, which is handled by the MS1 servlet and can be found by MS1 to the corresponding Serlet object news. Firstaction, or/newsmain->ms1->news. Firstaction, this is the meaning of the configuration file. To now understand the user/newsmain request will be news. Firstaction class objects to deal with, so say, to understand the program to understand the role of firstaction is what on the line. For example, here is an implementation of firstaction.
public final class FirstAction extends HttpServlet {
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
DB db = new DB();
HttpSession session = req.getSession();
try {
session.setAttribute(Constants.NEWS_LIST_KEY, News
.SearchNewsTitle(db));
} catch (Exception e) {
e.printStackTrace();
}
db.close();
String target = "/P43_News/newsMain.jsp";
resp.sendRedirect(target);
}
}
This implementation allows you to see that when the server receives a client request to perform news.searchnewstitle (db) operations, The return value is then placed through the Session.setattribute into the session and then through Resp.sendredirect (target) Indirect transfer to newsmain.jsp, so that the corresponding values stored in the session can be obtained through the Session.getattribute function in the newsmain.jsp.
Looking back is easy to see jsp+javabean two-layer structure and jsp+javabean+servlet three-layer structure, two-tier structure must be placed in the JSP, such as News.searchnewstitle (db), The three-layer structure of the first preprocessing in the servlet, and then equivalent to the processing results through the session back to the JSP, so that JSP more attention to the display of the interface.