Home > Web Front-end > JS Tutorial > Summary of value transfer methods between JSP pages

Summary of value transfer methods between JSP pages

韦小宝
Release: 2018-01-18 09:55:15
Original
2174 people have browsed it

Passing parameters between JSP pages is a function that is often used. Sometimes it is necessary to pass parameters between multiple JSP pages. The following article mainly introduces you to the relevant information about the method of passing values ​​between JSP pages. What is introduced in the article It is very detailed and has certain reference and learning value for everyone. Friends who are interested in JSP should read it together below.

Preface

Passing parameters between JSP pages is often required in projects. This should be regarded as a basic skill on the web. Try to summarize various methods, and if necessary, you can weigh the pros and cons and choose the most appropriate method. Let’s take a look at the detailed introduction:

1. Append parameters after the URL link

<a href="next.jsp?paramA=A&paramB=B..." rel="external nofollow" >URL 后面追加参数</a>

<jsp:include page="next.jsp"><jsp:param name="paramA" value="A"/></jsp:include> 

<jsp:forward page="next.jsp"><jsp:param name="paramA" value="B"/></jsp:forward> 

response.sendRedirect("next.jsp?paramA=A&paramB=B...")

window.location = "next.jsp?paramA=A&paramB=B..."
Copy after login

The above codes will be carried when executed The parameters jump to the next.jsp page.

The way to get the corresponding parameters in the next.jsp page is as follows:

//内嵌的 java 代码
<%String paramA = request.getParameter("paramA"); %>

//如果引入了 EL
{param.paramA}
Copy after login

Advantages: Simplicity and multi-browser support (no browsing The server does not support URLs).

Disadvantages:

1) The transmitted data can only be string, both of data type and size Certain restrictions;

2) The value of the transmitted data will be seen in the browser address bar, and the security level is low.

2. Form

 <form action="next.jsp" method="post"> 
  <input type="text" name="paramA" value="A"> 
  <input type="hidden" name="paramB" value="B"> 
  <input type="submit" value="提交"> 
 </form>
Copy after login

The method of obtaining the corresponding parameters in the next.jsp page is similar to (1).

Advantages:

1) Simplicity and multi-browser support (also no browser does not support form);

2) Can be submitted The amount of data is much larger than that of the URL method;

3) The transmitted value will be displayed in the address bar of the browser, but with some black tricks, the parameter list can also be constructed from the page source code;

Disadvantages:

1) The transmitted data can only be strings, and there are certain restrictions on the data type;

3. Set Cookie

Using the client's authentication credentials, a small cookie can of course also realize the value transfer of the JSP page.

 <% 
  Cookie c=new Cookie("paramA","A"); 
  c.setMaxAge(60*60); //cookie 有效期1分钟
  response.addCookie(c); //将cookie 添加到 http响应中
  %>
Copy after login

If you want to read cookies on the next.jsp page, you need to call the request.getCookies() method to obtain a javax.<a href="http://www.php.cn/wiki/1516.html" target="_blank">servlet</a>.http. Array of Cookie objects.

Then iterate through this array and use the getName() method and the getValue() method to get the name and value of each cookie.

//内嵌的 java 代码
<%
 Cookie cookie = null;
 // 获取cookies的数据,是一个数组
 Cookie[] cookies = request.getCookies();
 if(cookies != null ){
  for (int i = 0; i < cookies.length; i++){
   cookie = cookies[i];
   out.print("参数名 : " + cookie.getName());
   out.print("<br>");
   out.print("参数值: " + URLDecoder.decode(cookie.getValue(), "utf-8") +" <br>");
   out.print("------------------------------------<br>");
  }
 }else{
  out.println("<h2>没有发现 Cookie</h2>");
 }
%>

//EL 获取方式
${cookie.paramA.value}
Copy after login

Advantages:

1) The value of the cookie can be persisted. Even if the client machine is closed, the value inside can still be obtained next time it is opened;

2) Cookies can help the server save multiple state information, but do not require the server to allocate storage resources specifically, reducing the burden on the server.

Disadvantages:

1) Although the security is much improved compared to URL and Form, there are also black ways to obtain client cookies and expose customer information.

4. Setting Session

Personally, I think session and cookie are on the server side and one on the client side.

After adding key-value pairs to them, it not only provides transfer between pages, but is actually a data sharing solution.

 <%  
 session.setAttribute("paramA","A"); 
 response.sendRedirect("next.jsp"); 
 %>
Copy after login

How to read session in next.jsp:

//内嵌java 片段
<%=session.getAttribute("paramA") %>

//EL 获取方式
{session.paramA}
Copy after login

The advantages and disadvantages of Session can be referred to Cookie.

The above is all the content of this article, I hope it can help everyone learn! !

Related recommendations:

Detailed explanation of how to directly access the JSP page in the WEB-INF directory

How ajax jumps to a new one Implementation method of jsp page

Method of function label EL expression provided by JSTL in JSP to operate string

The above is the detailed content of Summary of value transfer methods between JSP pages. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template