JAVA-based Learning: Servlet and. NET General handlers in Model2 cannot be clearly understood. model2servlet

Source: Internet
Author: User

JAVA-based Learning: Servlet and. NET General handlers in Model2 cannot be clearly understood. model2servlet

After a few days, is it appropriate to have multiple days? It should be after more than a month. I think of the general processing program again, this is because jsp + servlet is often used in the recently implemented DRP system to achieve the separation of the interface and logic. servlet is responsible for processing the information returned from jsp. At this time, I feel that, the Servlet here seems to play the same role as the general processing program. However, because I do not have a deep understanding of the general processing program, I only applied it in a simple write instance, you can put forward any comments. Let's start with my limited knowledge to explain my understanding:

1. General processing program

Generally, the handler is used to process General web requests. Generally, the handler implements a FrameWork interface: IHttpHandler, which defines some system conventions that must be implemented to implement an HTTP riding bull. Create a blank general handler:

/// <Summary> /// HelloWord abstract description /// </summary> public class HelloWord: IHttpHandler {public void ProcessRequest (HttpContextcontext) {context. response. contentType = "text/plain"; context. response. write ("HelloWorld");} public bool IsReusable {get {return false ;}}}

We can see that you only need to implement one method and one attribute, where ProcessRequest places the main code for processing the request; IsReusable attribute, which indicates whether other requests can use the IHttpHandler instance, which is generally set to true;

The following uses a simple addition of users as an example to see how the general processing program is implemented:

Web transfer information:

// Add User information function saveOneOrganization () {// obtain the user name var nameValue =$ ("# UserName "). val (); // get the PassWord var passWordValue = $ ("# PassWord "). val (); // assign var test = "add" to the value of the hidden control test; if (nameValue = "" | passWordValue = "" |) {alert ("Please complete the information! ") ;}$. Post ("addManager. ashx ", {nameValue: nameValue, passWordValue: passWordValue, test: test}, function (data) {alert (data );});}


General information processing:

/// <Summary> /// addManager1 abstract description /// </summary> public class addManager1: IHttpHandler {UserManagerBLL userManagerBll = newUserManagerBLL (); public void ProcessRequest (HttpContextcontext) {// accept the test value passed from the front-end string command = context. request. params ["test"]; if (command = "addUser") // Add username {addUser (context);} else if (command = "delUser ") {modifyUser (context) ;}/// Add User Information public void addUser (HttpContextcontext) {// user name string strNameValue = context. request. params ["nameValue"]; // user password string strpassWordValue = context. request. params ["passwordValue"]; // instantiate the user entity UserEntity userEntity = new UserEntity (); // pass the variable value to the entity userEntity. userName = strNameValue; userEntity. password = strpassWordValue; // call the method string strResult = userManagerBll at Layer B. add (userEntity); // forward the value of context. response. write (strResult); context. response. end () ;}// modify public void modifyUser (HttpContextcontext) {// The implementation and addition of the user are not described in detail} public bool IsReusable {get {return true ;}}}


 

Ii. Servlet

In fact, Servelet has a wide range. The Servlet I mentioned here is only a Servlet implementation. When creating a Servlet to implement HttpServlet, similar to IHttpHandler, HttpServlet is easy to respond to Web client requests, the WEB Container encapsulates the customer request into an HttpServletRequest object, and then transmits the object to the Servlet object method for processing.

When inheriting HttpServlet, you can choose to overwrite some methods, such as doGet () or doPost (), or overwrite the Service method.

The following simple example shows how to add and modify an instance to view the servlet implementation:

Jsp call:

  function add(){    window.self.location="servlet/flowCard/FlowCardServlet?command=add"    }

Servlet implementation:

Publicclass UserServlet extends HttpServlet {@ Overrideprotectedvoid service (HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {// The service method super that calls the parent class must be displayed. service (request, response); if ("add ". equals (getCommand () {add (request, response);} elseif ("del ". equals (getCommand () {} elseif ("modify ". equals (getCommand ())) {} else {}}/*** add * @ param request * @ param response * @ throws ServletException * @ throws IOException */privatevoid add (HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {// user name String userName = request. getParameter ("userName"); // user passWord String passWord = request. getParameter ("passWord"); user. setUserName (userName) user. setpassWord (passWord) UserManager. add (user); // page Jump response. sendRedirect (request. getContextPath () + "/flowcard/flow_card_maint.jsp ");} /*** Delete * @ param request * @ param response * @ throws ServletException * @ throws IOException */privatevoid del (HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {// omitted} the servlet must be stored on the web. configure in xml: <servlet> <servlet-name> UserServlet </servlet-name> <servlet-class> com. tgb. userServlet </servlet-class> <load-on-startup> 2 </load-on-startup> </servlet> <servlet-mapping> <servlet-name> UserServlet </ servlet-name> <url-pattern>/servletUserServlet <url-pattern> </servlet-mapping>


Comparison:

1. In fact, from the code above, both of them can determine the method to be executed based on the parameters passed over the page, and then call the specific processing process at the business layer, it is easy to separate the page from the business logic.

2. In. the general handler in net and the Servlet in java are used to process the requests sent by the web end, and the coupling between the page and the business logic is unlocked. It looks like the role of the Controller in the MVC framework, recently, I feel a lot of things are very similar, mainly because the lack of practice has not been fully understood. please correct me.

 

 


What is the difference between Model1 and Model2 in java?

Model1 is developed using jsp + javabean, and Model2 is developed in MVC mode. I think I can find a lot of more detailed answers to this question on google. Why don't I search it myself ?!

What are Model1 and Model2? Explain their respective characteristics

Jsp + javabean and jsp + servlet + javabean are two modes. The former is pure JSP and the latter is simple MVC,
SUN has successively introduced two specifications for the MVC model. The first is JSP MODEL1, and the second is JSP MODEL2.
MODEL1 implements MVC in a certain program, that is, JSP is the combination of the control layer and the presentation layer, and BEAN is the pattern layer. this effect is obviously better than dividing the control layer separately. MODEL2 separately divides the control layer (SERVLET) and is responsible for controlling business processes, accepting page requests, and creating the required JAVABEAN instance, and return the processed data to JSP. This effect is much better.
Simply put, the coupling degree is tight...
Unless the project is very simple, we recommend that you use MODEL2. The control layer and the presentation layer should be mixed, Which is annoying.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.