JSP的request对象实例详解(jsp的软件)全程干货

随心笔谈2年前发布 admin
209 0 0

文章摘要

这篇文章详细描述了一段JavaServer Pages(JSP)代码,展示了如何从服务器获取和显示与网页相关的各种信息。代码通过`request`对象获取了用户名、密码、MIME类型、协议版本、服务器信息、请求长度、IP地址、真实路径以及上下文路径等信息,并通过JSP标签实现动态内容的显示。文章的核心内容在于展示如何利用JSP技术与HTTP客户端进行交互,以便实现网页功能的动态化和个性化展示。


<%@ 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=”https://www.jb51.net/article/<%=basePath%>” rel=”external nofollow” >

<title>My JSP ‘index.jsp’ starting page</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=”https://www.jb51.net/article/styles.css” rel=”external nofollow” >
–>
</head>

<body>
<h1>request内置对象</h1>
<%
request.setCharacterEncoding(“utf-8”); //解决中文乱码问题,无法解决URL传递中文出现的乱码问题。
request.setAttribute(“password”, “123456”);

%>
用户名:<%=request.getParameter(“username”) %><br>
爱好 :<%
if(request.getParameterValues(“favorite”)!=null)
{
String[] favorites=request.getParameterValues(“favorite”);
for(int i=0;i<favorites.length;i++)
{
out.println(favorites[i]+”  “);
}
}
%> <br>
密码:<%=request.getAttribute(“password”) %><br>
请求体的MIME类型:<%=request.getContentType() %><br>
协议类型及版本号: <%=request.getProtocol() %><br>
服务器主机名 :<%=request.getServerName() %><br>
服务器端口号:<%=request.getServerPort() %><BR>
请求文件的长度 :<%=request.getContentLength() %><BR>
请求客户端的IP地址:<%=request.getRemoteAddr() %><BR>
请求的真实路径:<%=request.getRealPath(“request.jsp”) %><br>
请求的上下文路径:<%=request.getContextPath() %><BR>
</body>
</html>

© 版权声明

相关文章