Example analysis of various methods of the request object in Java

  • 2020-04-01 04:34:13
  • OfStack

This article illustrates the use of various methods of the request object in Java. Share with you for your reference, as follows:

The request object makes a request from the client to the server, including the information submitted by the user and some information from the client. The request object is javax.mail servlet. HTTP. It implementation instance of a class.

The request object encapsulates the request information of the browser. Through various methods of the request object, the request information submitted by the client and the user can be obtained.

The following methods are commonly used to obtain the request parameters submitted by the client by using the request object:

1.String getParameter(String name), which gets the parameter value of the client and returns the value of the specified parameter as a String , returns a null value if the parameter does not exist. Use this method when passing parameters with a form, link, or url bar.

For example, get the parameter value of the client name:

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

2.String[] getParameterValues(String name), which gets all parameter values for a single parameter, mainly used to get the value of the check box , the return value type is String array String[]

For example, get all values for the client hobby check box:


String[ ] hobbys = request.getParameterValues("hobby");
if(hobbys != null)
{
out.println(" Your hobbies are: ");
for(int i=0;i<hobbys.length;i++)
 out.println(hobbys[i]);
}

3. Void setCharacterEncoding(String encoding), which sets the encoding mode of characters to solve the problem of garbling when passing non-english characters .

For example,

request.setCharacterEncoding("UTF-8");

Example: use the request object for user registration

The zhuce.html source code is as follows:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 <title> Personal information registration </title>
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="this is my page">
 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
 <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
 </head>
 <body>
 <h1 align="center"> Personal information registration </h1>
 <form action="zhuce.jsp" method="post">
   Name: <input type="text" name="name"><br>
   Password: <input type="password" name="pwd"><br>
   Please choose your occupation: 
  <input type="radio" name="career" value=" farmers "> farmers 
  <input type="radio" name="career" value=" workers "> workers 
  <input type="radio" name="career" value=" students " checked> students 
  <input type="radio" name="career" value=" Teachers' "> Teachers' 
  <br>
   Your favorite city: 
  <select name="city">
  <option value=" Liaoning province "> Liaoning province </option>
  <option value=" Hubei province "> Hubei province </option>
  <option value=" Henan province "> Henan province </option>
  <option value=" In shandong province "> In shandong province </option>
  <option value=" Jiangsu province "> Jiangsu province </option>
  <option value=" Hunan province " selected> Hunan province </option>
  </select>
  <br>
   Please choose your hobby: 
  <input type="checkbox" name="hobby" value=" tourism "> tourism 
  <input type="checkbox" name="hobby" value=" Reading a book " checked> Reading a book 
  <input type="checkbox" name="hobby" value=" The game "> The game 
  <input type="checkbox" name="hobby" value=" Unique romance "> Unique romance 
  <br>
   Self-introduction: 
  <textarea name="intro"> To introduce myself </textarea>
  <br>
  <input type="submit" name="submit" value=" submit ">
 </form>
 </body>
</html>

The zhuce.jsp source code is as follows:


<%@ page language="java" import="java.util.*" contentType="text/html;charset=UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 <base href="<%=basePath%>">
 <title> Personal information registration </title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0"> 
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
 </head>
 <body>
 <%request.setCharacterEncoding("UTF-8"); %>
   Your name is: <%=request.getParameter("name") %><br>
   Your password is: <%=request.getParameter("pwd") %><br>
   Your occupation is: <%=request.getParameter("career") %><br>
   Your favorite city is: <%=request.getParameter("city") %><br>
   Your hobbies are: <%String[] hobbys = request.getParameterValues("hobby");
  if(hobbys != null)
  {
  out.println(" Your hobbies are: ");
  for(int i=0;i<hobbys.length;i++)
   out.print(hobbys[i]);
  }
  %>
  <br>
   Self-introduction: <%=request.getParameter("intro") %><br>
 </body>
</html>

I hope this article has been helpful to you in Java programming.


Related articles: