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 Request and Response in JSP built-in objects

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how to use Request and Response in JSP built-in objects. It is very detailed and has a certain reference value. Friends who are interested must finish reading it!

Request object of JSP built-in object

The request information of the client is encapsulated in the request object, through which we can understand the customer's needs and then respond. It is an instance of the HttpServletRequest class.

The method of serial number is stated.

1 object getAttribute (String name) returns the property value of the specified property

2 Enumeration getAttributeNames () returns an enumeration of all available property names

3 String getCharacterEncoding () returns the character encoding method

4 int getContentLength () returns the length of the request body (in bytes)

5 String getContentType () gets the MIME type of the request body

6 ServletInputStream getInputStream () gets the binary stream of one line in the request body

7 String getParameter (String name) returns the parameter value of the parameter specified by name

8 Enumeration getParameterNames () returns an enumeration of available parameter names

9 String [] getParameterValues (String name) returns an array containing all values of the parameter name

10 String getProtocol () returns the protocol type and version number used for the request

11 String getScheme () returns the name of the plan used for the request, such as http.https, ftp, etc.

12 String getServerName () returns the hostname of the server that accepted the request

13 int getServerPort () returns the port number used by the server to accept this request

14 BufferedReader getReader () returns the decoded request body

15 String getRemoteAddr () returns the IP address of the client that sent this request

16 String getRemoteHost () returns the hostname of the client that sent this request

17 void setAttribute (String key,Object obj) sets the property value of the property

18 String getRealPath (String path) returns the real path of a virtual path

< %@ page contentType="text/html;charset=gb2312"%>

< %request.setCharacterEncoding("gb2312");%>

< html>

< head>

< title>

Request object _ example 1

< /title>

< /head>

< body bgcolor="#FFFFF0">

< form action="" method="post">

< input type="text" name="qwe">

< input type="submit" value="提交">

< /form>

Request method:

< %=request.getMethod()%>

< br>

Requested resources:

< %=request.getRequestURI()%>

< br>

The protocol used for the request:

< %=request.getProtocol()%>

< br>

Requested file name:

< %=request.getServletPath()%>

< br>

IP of the requested server:

< %=request.getServerName()%>

< br>

Port of the request server:

< %=request.getServerPort()%>

< br>

Client IP address:

< %=request.getRemoteAddr()%>

< br>

Client hostname:

< %=request.getRemoteHost()%>

< br>

The value submitted by the form:

< %=request.getParameter("qwe")%>

< br>

< /body>

< /html>

< %@ page contentType="text/html;charset=gb2312"%>

< %request.setCharacterEncoding("gb2312");%>

< %@ page import="java.util.Enumeration"%>

< html>

< head>

< title>

Request object _ example 2

< /title>

< /head>

< body bgcolor="#FFFFF0">

< form action="" method="post">

User name:

< input type="text" name="username">

Secret code:

< input type="text" name="userpass">

< input type="submit" value="进入" >

< /form>

< % String str=""; if(request.getParameter("username")!=null && request.getParameter("userpass")!=null){ Enumeration enumt = request.getParameterNames(); while(enumt.hasMoreElements()){ str=enumt.nextElement().toString(); out.println(str ":" request.getParameter(str) "< br>

");}} >

< /body>

< /html>

< %@ page contentType="text/html;charset=gb2312"%>

< %request.setCharacterEncoding("gb2312");%>

< html>

< head>

< title>

Request object _ example 3

< /title>

< /head>

< body bgcolor="#FFFFF0">

< form action="" method="post">

Be good at:

< input type="checkbox" name="cb" value="ON1">

VC

< input type="checkbox" name="cb" value="ON2">

JAVA

< input type="checkbox" name="cb" value="ON3">

DELPHI

< input type="checkbox" name="cb" value="ON4">

VB

< br>

< input type="submit" value="进入" name="qwe">

< /form>

< % if(request.getParameter("qwe")!=null ){ for(int i=0;i< request.getParameterValues("cb").length;i ){ out.println("cb" i ":" request.getParameterValues("cb")[i] "< br>

");} out.println (request.getParameter (" qwe "));}% >

< /body>

< /html>

Response object of JSP built-in object

The response object contains information about responding to customer requests, but it is rarely used directly in JSP. It is an instance of the HttpServletResponse class.

The method of serial number is stated.

1 String getCharacterEncoding () returns the character encoding used for the response

2 ServletOutputStream getOutputStream () returns a binary output stream of the response

3 PrintWriter getWriter () returns an object that can output characters to the client

4 void setContentLength (int len) sets the response header length

5 void setContentType (String type) sets the MIME type of the response

6 sendRedirect (java.lang.String location) redirect the client's request

The above is all the content of the article "how to use Request and Response in JSP built-in objects". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow 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