Four methods of transferring parameters in JSP

Time:2019-10-8

Today, the teacher talked about four methods of transferring parameters in jsp. I think it’s a good summary for later use.

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>

Following are examples:

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/> 
           <! - A space in front of my hobby is for the sake of good typesetting - > 
           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 () to accept data from an array 
        %> 
        <% 
          if(!name.equals("") && !pwd.equals("")) 
          { 
        %> 
            Hello! Log in successfully! <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>

Be careful:The form form is submitted to get, and it will encounter Chinese garbled when the parameters are passed. A simple solution is to convert the accepted string into a byte array and construct a new encoding format String with String, such as:

1. String name=request.getParameter(“name”); 

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

If the form form is submitted to post, the simple way to solve the garbled problem is to use request.setCharacterEncoding (“GB2312”); set the encoding method of request.

Why is there any Chinese garbled problem? Because the default encoding of the Tomcat server is iso-8859-1, when you pass the parameters to the server, the default iso-8859-1 encoding is used. However, when the server returns to you, it is encoded according to the page directive, such as: <%@page language= Java import=, java.util.*, pageEncoding=, GB2312%, so that the two coding methods are mixed. In order to get messy code, the solution is to transmit and receive code uniformly.

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 ("Passed parameters are:"+request. getAttribute ("name"); 
        %> 
      </body> 
    </html>

Request. setAttribute () and request. getAttribute () are implemented in conjunction with < jsp: forward > or include instructions.

3. Hyperlink: < 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 the get method of form form form. It passes parameters through the address bar, and its scrambling solution is the same as the get method 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 has been discovered here. In the case of Chinese garbled, in the case of form forms, if the way of delivery is post, it is necessary to set the encoding mode of request on the page of the receiving parameter, that is, request.setCharacterEncoding (“GB2312”); note that the page that receives the parameters will not work if the sentence is put in the form form. It’s still a mess. In this case, in order to make the passed parameters without garbled, it is request.setCharacterEncoding (“GB2312”); placed in the page of the sending parameters, will display the Chinese language normally and put it in the page of the receiving parameter, which will not work. Maybe that’s where < jsp: param > and form form pass parameters differ. Why is there such a difference? Perhaps it’s because the parameters in the form form are transmitted from the client to the server and need to go through a request packaging process, but the parameters passed by < jsp: param > are themselves on the server side and do not need to go through such a process from the client to the server, but what about parameter passing in the server? I don’t know about this problem. Really knowledge is an enlarged circle. The more you know, the more you don’t know! Try hard!

Above is the whole content of this article, I hope that the content of this article can bring some help to everyone’s study or work, but also hope to support more developpaer!