Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use get and post methods for passing parameters in JSP pages

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Xiaobian to share with you JSP page Chinese parameter transfer get and post method how to use, I believe most people still do not know how to use, so share this article for your reference, I hope you read this article after a great harvest, let us go to understand it together!

In projects, we often encounter the need to pass Chinese characters in JSP page transitions. There are two main ways to do this.

◆URL method

For example:

http://website/test1.jsp? act=add&type= apple ¶m=%20D%20B

◆FORM mode

For example:

﹤form name=test mehtod="post" input type=hidden name=text2 value="Chinese" input type=text name=text1 input type=submit value=submit/form

We will provide solutions to correct Chinese transmission for these two cases respectively.

JSP page Chinese parameter transmission 1: URL mode

For example:

http://website/test1.jsp? act=add&type= apple ¶m=%20D%20B

Generally speaking, we rarely write parameters in Chinese directly in URL, such as "type= apple" in the example. If this happens, we just need to do a simple conversion on our receive parameters page.

JSP page Chinese parameter transfer implementation code test1.jsp:(main part)

﹤%@ page language="java" import="java.util.* " pageEncoding="gb2312"%﹥ ﹤% String type = request.getParameter("type"); String result = new String(type.getBytes("iso-8859-1"), "gb2312"); out.println(result); %﹥

A more common practice is to encode Chinese characters in URLs into characters like type=%20D%20B.

JSP page Chinese parameter transfer implementation code MyJsp1.jsp:

﹤%@ page language="java" import="java.util.* " pageEncoding="gb2312"%﹥ ﹤%@ page import="java.net.* " %﹥ ﹤a href='./ MyJsp2.jsp? act= %=URLEncoder.encode("Chinese very good =-")%$>'> test/a>

JSP page Chinese parameter transfer code MyJsp2.jsp

﹤%@ page language="java" import="java.util.* " pageEncoding="gb2312"%﹥ ﹤%@ page import="java.net.* " %﹥ String tempVal = URLDecoder.decode(request.getParameter("act")); out.println(new String(tempVal.getBytes("ISO-8859-1"), "gb2312"));

JSP page Chinese parameter transmission 2: FORM mode

Please note that we are only discussing the Chinese case in the form of

, because Chinese can also be converted by this method when enctype="multipart/form-data" is parsed, so we will not repeat the discussion.

◆ form method=post This is the simplest case.

JSP page Chinese parameter transfer implementation code MyJsp1.jsp:

﹤%@ page language="java" import="java.util.* " pageEncoding="gb2312"%﹥ ﹤form action="./ MyJsp2.jsp" method="post" enctype="application/x-www-form-urlencoded"input type=hidden name=act value= action/input type=submit value=ok/form

JSP page Chinese parameter transfer implementation code MyJsp2.jsp:

﹤%@ page language="java" import="java.util.* " pageEncoding="gb2312"%﹥ request.setCharacterEncoding("gb2312"); out.println(request.getParameter("act"));

or

﹤%@ page language="java" import="java.util.* " pageEncoding="gb2312"%﹥ String tempVal = request.getParameter("act"); out.println(new String(tempVal.getBytes("ISO-8859-1"), "gb2312"));

◆ form method=get situation.

JSP page Chinese parameter transfer implementation code MyJsp1.jsp:

﹤%@ page language="java" import="java.util.* " pageEncoding="gb2312"%﹥ ﹤form action="./ MyJsp2.jsp" method="get" enctype="application/x-www-form-urlencoded"input type=hidden name=act value= action/input type=submit value=ok/form

JSP page Chinese parameter transfer implementation code MyJsp2.jsp:

﹤%@ page language="java" import="java.util.* " pageEncoding="gb2312"%> String tempVal = request.getParameter("act"); out.println(new String(tempVal.getBytes("ISO-8859-1"), "gb2312")); above is"JSP page Chinese parameter transfer get and post method how to use "All the contents of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report