Javaweb interaction process with JSP pages (initial contact write)

Source: Internet
Author: User

Javaweb and JSP page Interactive process javaweb project directory


2. JSP page is generally decentralized in the top (front page) back (background page)
3. Backstage code is placed below SRC, divided into:
1. DAO layer (database-related)
2. Domain layer (Entity layer)
3. Service tier (services tier)
4. servlet Layer

The foreground JSP uses the form or uses Js/jquery to send AJAX requests
 1. JSP sends request "<form action=" ${pagecontext.request.contextpath}/loginservlet "method=" Post "class via form table one-way servlet = "List" > <div class= "login_title" > Password login </div> ${msg} <input type= "text" class= "NA Me "name=" Phone "placeholder=" mobile number "> <input type=" password "name=" password "class=" password "> <in Put type= "Submit" class= "Submit" ></form> "note that the path inside the action is a servlet path 2.     The JSP sends a request to the servlet via jquery Ajax, which uses Ajax to send a request to PHP like this: "' var $value = this.value;     var $uid = $ ("#uid"). Val ();     var _url = "${pagecontext.request.contextpath}/checkpassword";     var _param = {"id": $uid, "password": $value}; $.post (_url, _param,function (data) {if (data = = 0) {$ ("#s1"). HTML ("<font color= ' red ' > Original password input error, please re-enter            </font> ");         $ ("#regBut"). Prop ("Disabled", true);     }else if (data = = 1) {$ ("#s1"). HTML ("<font color= ' green ' > original password entered correctly </font>");        $ ("#regBut"). Prop ("disabled", false);    }     }); ```
Background Java receives requests through the servlet first
1. The servlet layer first receives the request and then collects the data, instantiates it with the collected data to the last new service object of the entity object, and invokes a method of service to pass the instantiated object to a method of the service layer/** * Change Password *                   /public class Changepasswordservlet extends HttpServlet {private static final long serialversionuid = 1L;            /** * @see httpservlet#httpservlet () */public Changepasswordservlet () {            Super (); TODO auto-generated Constructor stub}/** * @see httpservlet#doget (httpservletrequest reques T, httpservletresponse response) */protected void doget (HttpServletRequest request, HttpServletResponse re Sponse) throws Servletexception, IOException {//Collect data String NewPassword = Request.getparameter ("New            Password ");            String id = request.getparameter ("id");            Package data User user = new user ();            User.setid (ID);            User.setpassword (NewPassword); Processing data UserService UserService = new UserSErvice ();                try {userservice.changepassword (user);                Adjust page request.setattribute ("msg", "Password modified successfully");            Request.getrequestdispatcher ("/top/success.jsp"). Forward (request, response);            } catch (SQLException e) {e.printstacktrace (); }}/** * @see httpservlet#dopost (httpservletrequest request, HttpServletResponse re Sponse) */protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletex        Ception, IOException {//TODO auto-generated Method stub doget (request, response); }} 2. A method of the service layer that receives an object directly from the new DAO layer object, will get the object passed to the layer of a method public class UserService {/** * Registered method * @param user * @thr OWS SQLException */public void ChangePassword (user user) throws SQLException {Userdao Userdao = new UserD        AO ();    Userdao.changepassword (user); }}3. Dao layer of a method to get the object, the database operation, and then encapsulate the result into one or more entities, return to the previous layer (depending on the situation, if you need to return the results of the execution results returned, if you do not need to return, you can not return, the default is the operation succeeds) public class Userdao {public void ChangePassword (user user) throws SQLException {Queryrunner Queryrunner = new Quer Yrunner (Jdbcutils.getdatasource ());//and database establish link String sql = "Update user set password =?" WHERE id =?            ";            Object[] params = {User.getpassword (), User.getid ()};        Queryrunner.update (sql, params); }}4.  If the result is required, the result is a layer of return, as to need not to instantiate the results into a view also needs to be based on the situation, to the servlet layer is recommended to return the JSON data format, usually contains code,msg and result three parts, For form submissions: The servlet layer typically uses Request.setattribute ("msg", "Hello"), and a similar method returns when it comes to logging in, use Request.getsession (). SetAttribute ("user", user), and then redirect the way that the Ajax submits the request: The servlet layer is generally used with response.getwriter (). Write ("0"), return data, The data that gets in the callback function in Ajax is what is returned.
The JSP page receives the results returned in the background
    1. If the form is submitted, you can write an expression of type ${msg} on the JSP page, and then redirect the servlet to a JSP page by using the Jstl tag and El expression to render the returned result in the JSP page.
    2. If it is an AJAX submission, you can modify the HTML DOM node or jump page directly using Js/jquery in the Ajax callback function.

Javaweb interaction process with JSP pages (initial contact write)

Related Article

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.