On the value-transfer problem of servlet and Jsp&java class

Source: Internet
Author: User

In order to click on the left navigation bar, dynamically changing the contents of the right side (a JSP file), you need to change the value of variable Var, so as to achieve the purpose of changing the contents of the default part (the way you think, because it is layman. There should be a quick and easy way, but I don't know. )

Describe the whole process in order to avoid forgetting later. Defines a Java method that defines the static variable var so that when Var is given a different value, default displays different content. The assignment is triggered by clicking on a <a>, which is the onclick. The question here is how to change the value of variables in Java in the JSP .... The solution is servelet. The following is the JS code in the JSP, the trigger condition is

<a href= "" Onclick=ajaxservlet ("list") >  News </a>

List is the value I want to pass to the variable var

<script>    var req;    function Ajaxservlet (pointinfo) {      //Get form submitted content      var url= "ajaxservlet?id=" +pointinfo;
Before representing the servlet name to be accessed, define it in the Web. xml file. The following is the value passed. console.log (URL); Creates a XMLHttpRequest object req if (window. XMLHttpRequest) { //ie7, Firefox, opera support req = new XMLHttpRequest (); } else if (window. ActiveXObject) { //ie5,ie6 support req = new ActiveXObject ("Microsoft.XMLHTTP"); } /* Open (String method,string URL, boolean) function has 3 parameters The method parameter specifies the methods used to send the request to the servlet, there are get,post, etc. The Boolean value specifies whether asynchronous, true is used, and false is not used. We use async to realize Ajax's powerful asynchronous capabilities. * /Req.open ("POST", url, True);
Console.log ("!!!!!!!!!!!!!!!!!!!!"); /Tune Trial
The onReadyStateChange property has a function to handle the server response, with 5 values representing different states
Req.onreadystatechange = callback; No return value required
The Send function sends the request req.send (NULL); } </script>

Here is the configuration of Servelet in the Web. xml file, with the name custom. The Java method created by class Url-pattern is a method that calls Servelet in a JSP or somewhere else, and can be any combination of strings that you define (should be).

  <servlet>        <servlet-name>AjaxServlet</servlet-name>        <servlet-class> model.ajaxservlet</servlet-class>    </servlet>    <servlet-mapping>        <servlet-name >AjaxServlet</servlet-name>        <url-pattern>/AjaxServlet</url-pattern>    </ Servlet-mapping>

And then. Java

Package model;/** * Created by Sinisiter on 2015/7/11. */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;public class Ajaxservlet extends HttpServlet {protected void ProcessRequest (Ht Tpservletrequest request, HttpServletResponse response) throws Servletexception, IOException {response.        setContentType ("Text/html;charset=utf-8");        PrintWriter out = Response.getwriter (); try {System.out.print ("NNNNN!!!!!");            /commissioning time, do not delete the Response.setcontenttype ("text/html");            Response.setheader ("Cache-control", "No-store");            Response.setheader ("Pragma", "No-cache");            Response.setdateheader ("Expires", 0); String name = Request.getparameter ("id");//Gets the value passed if (1>0) {test.pointr=name;//assigned to the variable I want after the Implement get and put method of God horse,I don't know yet.            However, it is now available} else {System.out.print ("NO!!!!!");        }} finally {Out.close (); }} @Override protected void doget (HttpServletRequest request, httpservletresponse response) throws Se    Rvletexception, IOException {processrequest (request, response); } @Override protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servle    Texception, IOException {processrequest (request, response);    } @Override Public String Getservletinfo () {return ' short description '; }}

 Ok. At this point, the whole work is over

Personally, the Servelet name is the same. Url-pattern is defined correctly, and the above JS is Ajax-style.

On the value-transfer problem of servlet and Jsp&java class

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.