Analysis of five common Servlet Methods

Source: Internet
Author: User

These five Servlet methods are powerful. I believe it will be helpful for WEB design. Do not experiment one by one. Wait until you finish reading all the content, and then use these methods together with the previous workshop to create a small program.

◆ ParseTextMessage () method

In this example, a data acceptance class is defined. The parseTextMessage () method accepts a set of objects transmitted from the form and their values. When we develop products, such as custom forms, we often do not know how many objects are transmitted. For such problems, the program cannot know. After using this method, it can be reported that all objects are obtained at once. Let's take a look at this example:

 
 
  1. Private void parseTextMessage (HttpServletRequest request ){
  2.  
  3. Enumeration names; // lists all objects transmitted by the client.
  4. String name ;/
  5. String [] value ;//
  6. // Accept all objects
  7. Names=Request. GetParameterNames ();
  8. While (names. hasMoreElements ()){
  9. // Get the Object Name
  10. Name= (String) names. nextElement ();
  11. // Find the object Value Based on the object name
  12. Value=Request. GetParameterValues (name );
  13. // Put the Object Name and value in a class. The code of this class is in the previous exercise
  14. Put (name, value );
  15. }
  16.  
  17. // Obtain the ID of a page. Why do you need to set the page id?
  18. PageID=GetPostPageID();
  19. // Obtain the transaction id. If a transaction exists
  20. TrxID=GetPostTrxID();
  21. }

◆ GetJSPBeanInSession () method

This is an I/O management class. This method returns jsp data from the session according to the page id. If getSession (true). getAttribute (pageID) is used, it is also correct.

 
 
  1. Private JPC_JSPBean getJSPBeanInSession (String pageID ){
  2.  
  3. The id of the data page to return jsp data from the session.
  4. Return (JPC_JSPBean) request. getSession (). getAttribute (pageID );
  5. }


◆ DispatchTransaction () method

This method gets a group of class instances and starts transaction processing.

 
 
  1. public void dispatchTransaction() {  
  2.     JPC_Transaction trx = null;  
  3.     String classList[] = getTrxClassList(getTrxID());  
  4.      try {     
  5.         for (int i = 0; i < classList.length; i++) {  
  6.               trx = (JPC_Transaction)Class.forName(classList[i]).newInstance();  
  7.             if (trx != null) {  
  8.                 trx.setIOManager(this);  
  9.                 trx.start();  
  10.             }  
  11.         }  
  12.     } catch (ClassNotFoundException e) {  
  13.         e.printStackTrace();  
  14.     } catch (InstantiationException e) {  
  15.         e.printStackTrace();  
  16.     } catch (IllegalAccessException e) {  
  17.         e.printStackTrace();  
  18.     }  


◆ PutJSPBeanInSession () method

Put the jsp data class and the ID of a jsp page into the session.

 
 
  1. Private void putJSPBeanInSession (String pageID, JPC_JSPBean jspBean ){
  2. // Put the jsp data class and the ID of a jsp page into the session.
  3. Request. getSession (). setAttribute (pageID, jspBean );
  4. }

◆ SendPage () method

 
 
  1. Private void sendPage (String pageID, JPC_JSPBean outJSPBean ){
  2.  
  3. // Store the page id and data in the session
  4. If (outJSPBean! = Null ){
  5. PutJSPBeanInSession (pageID, outJSPBean );
  6. }
  7.  
  8. // Obtain file data based on the file id
  9. StringJspFileName=GetJSPFileName(PageID );
  10.  
  11. // Obtain a RequestDispatcher Based on the jsp file name
  12. RequestDispatcherRd=Context. GetRequestDispatcher (jspFileName );
  13.  
  14. If (rd! = Null ){
  15.  
  16. Try {
  17. // Call jsp
  18. Rd. forward (request, response );
  19. } Catch (ServletException e ){
  20. } Catch (IOException e ){
  21. }
  22. }
  23. }

The use of Servlet methods should be accumulated in practice and summarized slowly. I hope you will be familiar with the use of Servlet methods as soon as possible.

  1. What are servlets and common Servlet APIs?
  2. Select JSP development tools
  3. Servlet and JSP paths
  4. Servlet Lifecycle
  5. Analysis of form data in JSP Servlet Technology

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.