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:
- Private void parseTextMessage (HttpServletRequest request ){
-
- Enumeration names; // lists all objects transmitted by the client.
- String name ;/
- String [] value ;//
- // Accept all objects
- Names=Request. GetParameterNames ();
- While (names. hasMoreElements ()){
- // Get the Object Name
- Name= (String) names. nextElement ();
- // Find the object Value Based on the object name
- Value=Request. GetParameterValues (name );
- // Put the Object Name and value in a class. The code of this class is in the previous exercise
- Put (name, value );
- }
-
- // Obtain the ID of a page. Why do you need to set the page id?
- PageID=GetPostPageID();
- // Obtain the transaction id. If a transaction exists
- TrxID=GetPostTrxID();
- }
◆ 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.
- Private JPC_JSPBean getJSPBeanInSession (String pageID ){
-
- The id of the data page to return jsp data from the session.
- Return (JPC_JSPBean) request. getSession (). getAttribute (pageID );
- }
◆ DispatchTransaction () method
This method gets a group of class instances and starts transaction processing.
- public void dispatchTransaction() {
- JPC_Transaction trx = null;
- String classList[] = getTrxClassList(getTrxID());
- try {
- for (int i = 0; i < classList.length; i++) {
- trx = (JPC_Transaction)Class.forName(classList[i]).newInstance();
- if (trx != null) {
- trx.setIOManager(this);
- trx.start();
- }
- }
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (InstantiationException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- }
- }
◆ PutJSPBeanInSession () method
Put the jsp data class and the ID of a jsp page into the session.
- Private void putJSPBeanInSession (String pageID, JPC_JSPBean jspBean ){
- // Put the jsp data class and the ID of a jsp page into the session.
- Request. getSession (). setAttribute (pageID, jspBean );
- }
◆ SendPage () method
- Private void sendPage (String pageID, JPC_JSPBean outJSPBean ){
-
- // Store the page id and data in the session
- If (outJSPBean! = Null ){
- PutJSPBeanInSession (pageID, outJSPBean );
- }
-
- // Obtain file data based on the file id
- StringJspFileName=GetJSPFileName(PageID );
-
- // Obtain a RequestDispatcher Based on the jsp file name
- RequestDispatcherRd=Context. GetRequestDispatcher (jspFileName );
-
- If (rd! = Null ){
-
- Try {
- // Call jsp
- Rd. forward (request, response );
- } Catch (ServletException e ){
- } Catch (IOException e ){
- }
- }
- }
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.
- What are servlets and common Servlet APIs?
- Select JSP development tools
- Servlet and JSP paths
- Servlet Lifecycle
- Analysis of form data in JSP Servlet Technology