Four Methods of Passing Parameters in jsp

  • 2021-12-05 07:00:27
  • OfStack

Today, the teacher talked about four methods of transferring parameters in jsp. I think it is quite good to summarize 1, so as to prepare for later use!

1. form form

2. request. setAttribute (); And request. getAttribute ();

3. Hyperlinks: < a herf="index.jsp"?a=a & b=b & c=c > name < /a >

4. < jsp:param >

Here are 11 examples:

1. form form

form.jsp:


    <%@page contentType="text/html; charset=GB2312"%> 
    <html> 
      <head> 
        <title> 
          form.jsp file 
        </title> 
      </head> 
      <body style="background-color:lightblue"> 
        <h2 style="font-family:arial;color:red;font-size:25px;text-align:center"> Login page </h2> 
        <form action="result.jsp" method="get" align="center"> 
           Name: <input type="text" name="name" size="20" value="" maxlength="20"><br/> 
           Password: <input type="password" name="password" size="20" value="" maxlength="20"><br/> 
           <!-- Empty before hobbies 1 The space is for the typesetting to look better --> 
          &nbsp; Hobbies: <input type="checkbox" name="hobby" value=" Singing "> Singing  
             <input type="checkbox" name="hobby" value=" Football "> Football  
             <input type="checkbox" name="hobby" value=" Basketball "> Basketball <br/><br/> 
          <input type="submit" name="submit" value=" Login "> 
          <input type="reset" name="reset" value=" Reset "><br/> 
        </form> 
      </body> 
    </html> 

result.jsp:


    <%@page language="java" import="java.util.*" pageEncoding="GB2312"%> 
    <html> 
      <head> 
        <title> 
          result.jsp file 
        </title> 
      </head> 
      <body bgcolor="ffffff"> 
        <% 
         request.setCharacterEncoding("GB2312"); 
         String name=request.getParameter("name"); 
         name=new String(name.getBytes("iso-8859-1"),"GB2312"); 
         String pwd=request.getParameter("password"); 
         String[] hobby=request.getParameterValues("hobby");// Note that the function here is getParameterValues() Accept 1 Array of data  
        %> 
        <% 
          if(!name.equals("") && !pwd.equals("")) 
          { 
        %> 
             Hello! Login successful! <br/> 
             Name: <%=name%><br/> 
             Password: <%=pwd%><br/> 
             Hobbies: <% 
                 for(String ho: hobby) 
                 { 
                  ho=new String(ho.getBytes("iso-8859-1"),"GB2312"); 
                  out.print(ho+" "); 
                 } 
                %> 
        <% 
          } 
          else 
          { 
        %> 
               Please enter your name or password!  
        <% 
         } 
        %> 
      </body> 
    </html> 

Note: The form form is submitted in get, and the problem of Chinese garbled code will be encountered when the parameters are passed. A simple solution is to convert the received string into an byte array first, and then use String to construct a new String encoding format, such as:

1. String name=request.getParameter("name");

2. name=new String(name.getBytes("iso-8859-1"),"GB2312");

If the form form is submitted as post, the simple solution to the garbled problem is to use request. setCharacterEncoding ("GB2312"); Set the encoding mode of request.

Why is there a problem of Chinese garbled codes? Because the default system encoding mode of Tomcat server is iso-8859-1, when you pass parameters to the server, you use the default encoding mode of iso-8859-1, but when the server returns information to you, it is according to the encoding mode set in page instruction, such as: < %@page language="Java" import="java.util.*" pageEncoding="GB2312"% > In this way, the two coding methods are mixed, so there will be garbled codes, so the solution is to unify the coding methods of transmission and reception.

2. request. setAttribute () and request. getAttribute ()

set.jsp:


    <%@page contentType="text/html; charset=GB2312"%> 
    <html> 
      <head> 
        <title> 
          set.jsp file 
        </title> 
      </head> 
      <body style="background-color:lightblue"> 
        <% 
          request.setAttribute("name"," Heart rain "); 
        %> 
        <jsp:forward page="get.jsp"/> 
      </body> 
    </html> 

get.jsp:


    <%@page contentType="text/html; charset=GB2312"%> 
    <html> 
      <head> 
        <title> 
          get.jsp file 
        </title> 
      </head> 
      <body style="background-color:lightblue"> 
        <% 
         out.println(" The parameters passed in are: "+request.getAttribute("name")); 
        %> 
      </body> 
    </html> 

request. setAttribute () and request. getAttribute () are coordination < jsp:forward > Or include instruction.

3. Hyperlinks: < a herf="index.jsp?a=a & b=b & c=c" > name < /a >

href.jsp:


    <%@page contentType="text/html; charset=GB2312"%> 
    <html> 
      <head> 
        <title> 
          href.jsp file 
        </title> 
      </head> 
      <body style="background-color:lightblue"> 
        <a href="getHerf.jsp?name= Heart rain &password=123" rel="external nofollow" > Transfer parameter </a> 
      </body> 
    </html> 

getHref.jsp:


    <%@page contentType="text/html; charset=GB2312"%> 
    <html> 
      <head> 
        <title> 
          getHref.jsp file 
        </title> 
      </head> 
      <body style="background-color:lightblue"> 
        <% 
          String name=request.getParameter("name"); 
          name=new String(name.getBytes("iso-8859-1"),"gb2312"); 
          out.print("name:"+name); 
        %> 
        <br/> 
        <% 
          out.print("password:"+request.getParameter("password")); 
        %> 
      </body> 
    </html> 

This method of passing parameters is similar to get mode of form form, which is passed through address bar, and its garbled solution is also similar to get mode 1 of form.

4. < jsp:param >

param.jsp:


    <%@page contentType="text/html; charset=GB2312"%> 
    <html> 
      <head> 
        <title> 
          param.jsp file 
        </title> 
      </head> 

      <body style="background-color:lightblue"> 

        <%request.setCharacterEncoding("GB2312");%> 
        <jsp:forward page="getParam.jsp"> 
          <jsp:param name="name" value=" Heart rain "/> 
          <jsp:param name="password" value="123"/> 
        </jsp:forward> 
      </body> 
    </html> 

getParam.jsp:


    <%@page contentType="text/html; charset=GB2312"%> 
    <html> 
      <head> 
        <title> 
          getParam.jsp file 
        </title> 
      </head> 
      <body style="background-color:lightblue"> 
        <% 
          String name=request.getParameter("name"); 
          out.print("name:"+name); 
        %> 
        <br/> 
        <% 
          out.print("password:"+request.getParameter("password")); 
        %> 
      </body> 
    </html> 

A strange problem is found here, which is still the problem of Chinese garbled codes. In the example of form form, if the transmission mode is post, it is only necessary to set the encoding mode of request on the page receiving parameters, that is, request. setCharacterEncoding ("GB2312"); Note that it is on the page receiving parameters. If you put this sentence in the form form, it will not work and it will still be garbled. In this case, request. setCharacterEncoding ("GB2312") is added in order to prevent the passed parameters from being garbled; Put it on the page of sending parameters, it will display Chinese normally, and put it on the page of receiving parameters, it will not work. Maybe this is what it is < jsp:param > And form forms pass parameters differently. Why is there such a difference? It may be because the parameters in the form form are passed from the client to the server, which requires 1 request packaging process, but < jsp:param > The passed parameters themselves are on the server side, so there is no need to go through such a process from the client to the server side, but what about the parameter passing in the server? This problem, I don't know! It's true that knowledge is an enlarged circle. The more you know, the more you don't know! Work hard!


Related articles: