当前位置:网站首页>JSP built-in object (implicit object) -- input / output object
JSP built-in object (implicit object) -- input / output object
2022-07-26 07:58:00 【Super Qi】
jsp Implicit objects —— Input / Output object
1. effect
It mainly controls page output and input , Access information related to all requests and responses
2. request object

a. effect
Saved the information submitted by the user , The information submitted by the user can be obtained by calling the corresponding method of the object
b. Common methods
1.String getParameter(String name);
2.String getParameterValues(String name);
3.Enumeration getParameterNames();
4.setAttribute(String name,Object o);
5.Object getAttribute(String name);
6.Session getSession(String name);
7.removeAttribute(String name);
8.setCharacterEncoding // Set the encoding of the request data , Only applicable to post Method
9.getRequestDispatcher(String path);
c. Access request parameters
Get information submitted by the client ,request Object represents the request from the client
1. String getParameter("XX"); // obtain name Parameter values for
2. String[] getParameterValues("XX"); // obtain name All parameter values
3. String[] getParameterNames(); // Get all parameter names
example :getParameter(“XX”) The return type is String
/***requestInfo.jsp***/
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title> Input data interface </title>
</head>
<body>
<form action="requestInfo_deal.jsp">
user name :<input type="text" name="username"><br>
The secret code :<input type="password" name="password"><br>
<input type="submit" value=" Submit ">
<input type="submit" value=" Cancel ">
</form>
</body>
</html>
/***requestInfo_deal.jsp***/
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title> Processing data pages </title>
</head>
<body>
<h1> What you just entered is :</h1>
<%
String name = request.getParameter("username");
String password = request.getParameter("password");
out.println(" full name : Parameter name :"+name);
out.println(" password : Parameter name :"+password);
%>
</body>
</html>
example :getParameterValues(“XX”) The return type is String[]
/***requestProduct.jsp***/
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1> Your hobby is :</h1>
<form action="requestDeal.jsp" method="post">
<input type="checkbox" name="chekBox" value="Basketball"> To play basketball
<input type="checkbox" name="chekBox" value="Football"> play football
<input type="checkbox" name="chekBox" value="Code"> Write code
<input type="checkbox" name="chekBox" value="Games"> Play the game
<br>
<input type="submit" value=" Submit ">
<input type="reset" value=" eliminate ">
</form>
</body>
</html>
/***requestDeal.jsp***/
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<html>
<head>
<title> Data processing </title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String[] chekBoxes = request.getParameterValues("chekBox");
out.print(" What are your hobbies :");
if (chekBoxes!=null){
for (int i = 0;i<chekBoxes.length;i++){
if (chekBoxes[i].equals("Basketball")){
out.print(" To play basketball ");
}
if (chekBoxes[i].equals("Football")){
out.print(" play football ");
}
if (chekBoxes[i].equals("Code")){
out.print(" Write code ");
}
if (chekBoxes[i].equals("Games")){
out.print(" Play the game ");
}
}
}
%>
</body>
</html>
example :getParameterNames() The return type is enumeration type Enumeration
/***requestInfo.jsp***/
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title> Input data interface </title>
</head>
<body>
<form action="requestDemo2.jsp">
user name :<input type="text" name="username"><br>
The secret code :<input type="password" name="password"><br>
<input type="submit" value=" Submit ">
<input type="submit" value=" Cancel ">
</form>
</body>
</html>
/***requestDemo2.jsp***/
<%
Enumeration enumeration = request.getParameterNames();// Get yes name Parametric value
while (enumeration.hasMoreElements()){ //enumeration.hasMoreElements() obtain Enumeration Number of parameters in
String pName = (String)enumeration.nextElement();
String pValue = request.getParameter(pName);
out.print(" Parameter name "+ pName+"<br>");
out.print(" Parameter contents "+ pValue+"<br>");
}
%>
</body>
</html>
* If there is Chinese garbled code on the page , stay request.getParameter() prefix request.setCharacterEncoding(“UTF-8”);
d. Manage properties in scope
When forwarding , The data that needs to be forwarded will be brought to the forwarding page for processing , use
request.setAttribute("key",Object);//key Key for value transmission , Used to get parameter values , by String type .Object It needs to be saved to request Inside
Get this data on the forwarding page with
request.getAttribute("key");
example :
<body>
<%
request.setAttribute("username"," Zhang San ");
request.setAttribute("hobby"," To play basketball ");
%>
<%
user name :<%=request.getAttribute("username"); %>
hobby :<%=request.getAttribute("hobby"); %>
%>
</body>
e. getParameter() And getAttribute() The difference between
- getParameter The return value is String, Get the value in the form submitted by the client
- getAttribute The return value is of any data type , Get the data set by the server

f. obtain Cookie
1. cookie The definition of
Cookie It's a small piece of text message , With user requests and pages Web Transmission between server and browser . You can save the user's login information
2. cookie obtain
Cookie[] cookie = request.getCookies();
g. Methods of obtaining customer information

3. response object
a. Definition :
response Object processing jsp Generate a response and send it to the client . After the transaction is processed response The object will be destroyed

b. Common methods
1. flushBuffer(); // Force all contents of the current buffer to be sent to the client
2. getBufferSize(); // Get the actual buffer size
3. getCharacterEncoding(); // Gets the character encoding format of the response
4. setCharacterEncoding(); // Set the character encoding format of the response
5. getContentType(); // Get the content of the response MIME type
6. setContentType(); // Set the content of the response MIME type
7. getOutputStream(); // Get the client's input stream
8. sendError(); // Send error message to client
9. setContentLength(); // Set the length of the response content ( Number of bytes )
c. response Redirect pages
1. effect
Redirect client requests to a different page
response.sendRedirect(login.jsp?param=aaa&...);
2.response.sendRedirect() And jsp:forward The difference between
a. response.sendRedirect(): Jump on the client , The browser will redirect according to url Initiate request .request and response Will regenerate
b. jsp:forward: Jump on the server , browser url unchanged ,request Cannot regenerate , So it can be used request To pass parameters

c. example
/***sendRedirect.jsp***/
<body>
<form mothod="post">
enter one user name :
<input type="text" name="name"/>
<input type="submit" value="login"/>
</form>
<%
String name = request.getParameter("name");
if(name!=null){
request.sendRedirect("redirect.jsp?sendname="+name);
//?sendname= It is to pass values to the page after resetting
out.print(" Reset backward statement !");// Used to distinguish jsp:forward
}
%>
</body>
/***redirect.jsp***/
<body>
<%
String sendname = request.getParameter("sendname"); // Receive the value transmitted by redirection
%>
user name :<%=sendname %>
</body>
d. Set up HTTP Respond to the headlines


example :
<%@ page contentType="text/html;charset=gb2312"%>
<body>
<%@include file="work.html"%>
<%
String str1 = request.getParameter("word");
String str2 = request.getParameter("excel");
if(str1!=null && str1.equals("word")){
response.setContentType("application/msword;charset=gb2312");
}
if(str2!=null && str2.equals("excel")){
response.setContentType("application/x-msexcel;charset=gb2312");
}
%>
</body>
<html>
<head>
<title>response.setContentType Example </title>
</head>
<body>
<p>
What kind of document is the current page saved as
</p>
<form method="get" name="form">
<input type="submit" value="word" name="word"/>
<input type="submit" value="excel" name="excel"/>
</form>
</body>
</html>
response.setHeader("refresh","time","url");//refresh For refresh ,time Time for refresh .url Jump to the page after refreshing
e. Set the status display code


f. Save client information
1. Method : addCookie(Cookie cookie);
2. obtain :request.getCookie() obtain
3. effect : Save user's personalized information , Provide convenience for the next visit
4.out object (JspWriter)
a. Definition
1. Represents the output stream ,PrintWriter type
2. out The object is javax.servlet.jsp.PrintWriter An object of , The browser that can send information to the client
3. Write Only information related to characters can be output ,print You can output any type of information
b. out Object method

c. out Characteristics of the object


边栏推荐
- 力扣(LeetCode)206. 反转链表(2022.07.25)
- Regression analysis code implementation
- [keras entry log (3)] sequential model and functional model in keras
- Common methods of string: construction method, other methods
- The difference between LinkedList and ArrayList
- AQS implementation principle
- 记一次路由器频繁掉线问题的分析、解决与发展
- Interview question set
- PHP environment deployment
- Wrong Addition
猜你喜欢

A tutorial for mastering MySQL database audit characteristics, implementation scheme and audit plug-in deployment

MMOE multi-objective modeling

Traversal mode of list, set, map, queue, deque, stack
![[classic thesis of recommendation system (10)] Alibaba SDM model](/img/a5/3ae37b847042ffb34e436720f61d17.png)
[classic thesis of recommendation system (10)] Alibaba SDM model

Kdd2022 | uncover the mystery of Kwai short video recommendation re ranking, and recommend the new SOTA

Master slave database deployment

Jmeter性能测试之使用存储响应内容到文件监听器

如何关闭高位端口

系统架构&微服务

程序环境和预处理
随机推荐
音视频学习(十)——ps流
No valid host was found when setting up openstack to create an instance There are not enough hosts available. code:500
Use of views
Program environment and pretreatment
System architecture & microservices
爬虫->TpImgspider
20220209 create a basic Servlet
Practice of online question feedback module (XIV): realize online question answering function
How to determine the authenticity of the website you visit -- certificate system
程序环境和预处理
LeetCode剑指offer专项(一)整数
What are the differences between FileInputStream and bufferedinputstream?
Machine learning related competition website
API (common class 2)
Summary of API method
Common database commands (special for review)
Next item recommendations in short sessions
Como automatic test system: build process record
Utils connection pool
Web page basic label