Analysis of multiple methods for calling Servlet

Source: Internet
Author: User
Tags websphere application server

To call a Servlet or Web application, use either of the following methods: servlet is called by URL, Servlet is called in the <FORM> mark, SERVLET is called in the <Servlet> mark, Servlet is called in the JSP file, and Servlet is called in the ASP file.

1. The Servlet is called by the URL

There are two methods to call the Servlet from the browser using the Servlet URL:

(1) Specify the Servlet name: when using the WebSphere Application Server Manager to add a Servlet instance for registration) in the server configuration, you must specify the value of the "Servlet name" parameter. For example, you can specify hi as the Servlet name of HelloWorldServlet. To call this Servlet, open http://your.server.name/servlet/hi. You can also specify the Servlet and class to use the same name HelloWorldServlet ). In this case, we will use http://your.server.name/servlet/helloworldservletto call the servlet的.

(2) Specify the Servlet alias: Use the WebSphere Application Server Manager to configure the Servlet alias, which is a shortcut URL used to call the Servlet. The Servlet name is not included in the shortcut URL.

2. Specify the Servlet to be called in the <FORM> flag.

You can call Servlet in the <FORM> flag. The HTML format allows you to input data on a Web page, that is, from a browser, and submit data to the Servlet. For example:

 
 
  1. <FORMMETHOD= "GET"ACTION= "/Servlet/myservlet">
  2. <OL>
  3. <INPUTTYPE= "Radio"NAME= "Broadcast"VALUE="Am"> AM <BR>
  4. <INPUTTYPE= "Radio"NAME= "Broadcast"VALUE="Fm"> FM <BR>
  5. </OL>
  6. Used to place the markup, buttons, and other prompts in the text input area .)
  7. </FORM>

The ACTION feature indicates the URL used to call the Servlet. For METHOD features, if the user input information is submitted to the Servlet through the get method, the Servlet must first use the doGet () METHOD. Otherwise, if the user input information is submitted to the Servlet through the POST method, the Servlet must first use the doPost () method. When the GET method is used, the information provided by the user is the URL encoding represented by the query string. You do not need to encode the URL, because this is done by the form. Then, the URL-encoded query string is appended to the ServletURL, And the whole URL is submitted completely. The URL encoded query string matches the value selected by the user with the name of the visible part based on the interaction between the user and the visible part. For example, the preceding HTML code segment is used to display buttons (marked as AM and FM). If you select the FM button, the query string will contain the name = value pair operation as broadcast = fm. In this case, the Servlet will respond to the HTTP request, so the Servlet should be based on the HttpServlet class. Servlet should use the GET or POST method based on the user information in the query string submitted to it, and use the doGet () or doPost () method accordingly.

3. Specify the SERVLET to be called in the <Servlet> tag.

When you use the <SERVLET> tag to call the Servlet, you do not need to create a complete HTML page, just like using the <FORM> tag. As an alternative, Servlet output is only part of the HTML page and is dynamically embedded into other static text in the original HTML page. All these occur on the server, and only the result HTML page is sent to the user. We recommend that you use the <SERVLET> tag in the JSP file of the Java Server Page. See JSP Technology

The original HTML page contains the <SERVLET> and </SERVLET> tags. The Servlet will be called in the two tags, and the Servlet response will overwrite everything between the two tags and the tag itself. If the user's browser can see the HTML source file, the user will not see the <SERVLET> and </SERVLET> tags. To use this method on DominoGoWebserver, enable the server-side functions on the server. Some enabling processes involve adding a special file type SHTML. When the Web server receives a Web page request with the SHTML extension, it will search for the <SERVLET> and </SERVLET> tags. For all supported Web servers, the WebSphere Application Server processes all information between SERVLET tags. The following HTML code snippet shows how to use this technology.

 
 
  1. ﹤SERVLET NAME="myservlet" CODE="myservlet.class" CODEBASE="url" initparm1="value"﹥  
  2. ﹤PARAM NAME="parm1" VALUE="value"﹥  
  3. ﹤/SERVLET﹥ 

The use of the NAME and CODE attributes makes it flexible to use. You can use only one of the attributes or both of them. The NAME attribute specifies the Servlet NAME configured using the WebSphere Application Server Manager), or the Servlet class NAME without the. class extension. The Servlet class name is specified in the CODE attribute. When using the WebSphere Application Server, we recommend that you specify the NAME and CODE, or specify only the NAME when the NAME specifies the Servlet NAME. If only CODE is specified, a Servlet instance with NAME = CODE will be created. The loaded Servlet will assume that the Servlet NAME matches the NAME specified in the NAME attribute. Then, other SHTML files can successfully use the NAME attribute to specify the Servlet NAME and call the mounted Servlet. The NAME value can be directly used in the URL of the Servlet to be called. If both NAME and CODE exist and NAME specifies an existing Servlet, the Servlet specified in NAME is usually used. Because the Servlet creates some HTML files, a subclass of HttpServlet may be used when the Servlet is created, and doGet () is preferred () method because the GET method provides information to the Servlet by default ). Another option is to prioritize the service () method. In addition, CODEBASE is optional. It specifies the URL of the remote system that loads the Servlet. Use the WebSphere Application Server Manager to configure remote Servlet loading from the JAR file to the system.

In the preceding tag example, initparm1 is the initialization parameter name and value is the value of this parameter. You can specify a set of multiple "name-value" pairs. Use the getInitParameterNames () and getInitParameter () Methods of the Servlet init () method to find the string array of the parameter name and parameter value. In this example, parm1 is the parameter name and is set only after Servlet initialization. Because the parameter set with the <PARAM> flag can only be used by using the method of the "request" object, the server must call the Servletservice () method to pass the request from the user. To obtain user request information, use the getParameterNames (), getParameter (), and getParameterValues () methods.

The initialization parameters are continuous. Assume that a client calls Servlet by calling a SHTML file containing some initialization parameters. Assume that the second client calls the same Servlet by calling the second SHTML file, and The SHTML does not specify any initialization parameters. The initialization parameter set when the Servlet is called for the first time will always be available and will not be changed by all subsequent servlets called through all other SHTML files. The initialization parameter cannot be reset until the Servlet calls the destroy () method. For example, if another SHTML file specifies another different initialization parameter value, although the Servlet has been mounted, this value will still be ignored.

4. Call Servlet in JSP file

Servlet can be called from the JavaServer Page JSP) file. See the JSP Technology Section.

5. Call Servlet in ASP file

If there are legacy ASP files on Microsoft Internet Information ServerIIS and ASP files cannot be transplanted into JSP files, you can use ASP files to call Servlet. ASP on the WebSphere Application Server supports ActiveX Control embedded in Servlet. The following describes the methods and attributes of ActiveX control over AspToServlet.

This method is described as follows:

(1) String ExecServletToString (String servletName); execute ServletName and return its output to a String.

(2) ExecServlet (String servletName); execute ServletName and send its output directly to the HTML page.

(3) String VarValue (String varName); obtain a preset variable value in other formats ).

(4) VarValue (String varName, String newVal); set the variable value. The total size of the variable should be less than 0.5 Kbyte ). And only use these variables for the configuration file.

Its Attributes are as follows:

◆ Boolean WriteHeaders; if this attribute is true, the title provided by the Servlet is written to the user. The default value is false.

◆ Boolean OnTest; if this attribute is true, the server records the message to the generated HTML page. The default value is false.

The following example of using ASP to call Servlet scripts is written in Microsoft Visual Basic ScriptingVBScript.

 
 
  1. ﹤%  
  2. ' Small sample asp file to show the capabilities of the servlets and the ASP GateWay ...  
  3. %﹥  
  4. ﹤H1﹥ Starting the ASP-﹥Java Servlet demo﹤/H1﹥  
  5. ﹤%  
  6. ' Create a Servlet gateway object and initialize it ...  
  7. Set javaasp = Server.CreateObject("AspToServlet.AspToServlet")  
  8. ' Setting these properties is only for the sake of demo.  
  9. ' These are the default values ...  
  10. javaasp.OnTest = False 
  11. javaasp.WriteHeaders = False 
  12. ' Add several variables ...  
  13. javaasp.VarValue("gal") = "lag"  
  14. javaasp.VarValue("pico")= "ocip"  
  15. javaasp.VarValue("tal") = "lat"  
  16. javaasp.VarValue("paz") = "zap"  
  17. javaasp.VarValue("variable name with spaces") = "variable value with spaces"  
  18. %﹥  
  19. ﹤BR﹥  
  20. Lets check the variables  
  21. ﹤%  
  22. Response.Write("variable gal = ")  
  23. Response.Write(javaasp.VarValue("gal"))  
  24. %﹥  
  25. ﹤BR﹥  
  26. ﹤%  
  27. Response.Write("variable picopico = " & javaasp.VarValue("pico"))  
  28. %﹥  
  29.  
  30. ﹤BR﹥  
  31. ﹤HR﹥  
  32. ﹤%  
  33. galout = javaasp.ExecServletToString("SnoopServlet")  
  34. If javaasp.WriteHeaders = True Then  
  35. %﹥  
  36. Headers were written ﹤%  
  37. Else  
  38. %﹥  
  39. Headers were not written ﹤%  
  40. End If  
  41. Response.Write(galout)  
  42. %﹥  
  43. ﹤H1﹥ The End ...﹤/H1﹥ 

The method for calling Servlet is also based on the requirements of the programmer. The above methods are helpful for calling Servlet at work.

  1. Test Servlet configuration and development
  2. Select JSP development tools
  3. Servlet and JSP paths
  4. How to Improve Servlet and JSP Application Efficiency
  5. Functions and principles of several encodings in JSP and Servlet

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.