JSP之Request对象
程序员文章站
2022-06-11 15:53:32
...
index.html代码如下
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>My JSP 'index.html' starting page</title>
</head>
<body>
<form action="request.jsp" method="get">
<input type="text" name="txt"><br>
<input type="text" name="txt2"><br>
<select name="select">
<option value="软件01">软件01</option>
<option selected="selected" value="软件02">软件02</option>
<option value="软件03">软件03</option>
</select>
<br>
<input type="radio" name="rad1" value="out" >out
<input type="radio" name="rad1" checked="checked" value="request">request
<input type="radio" name="rad1" value="response">response
<br>
<input type="submit" name="submit" value="提交表单">
<input type="reset" name="reset">
</form>
</body>
</html>
request.jsp代码
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page language="java" import="com.Change" %>
<%
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>My JSP 'request.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="styles.css">
-->
</head>
<body>
获取文本框1的值 :
<% //先获取inputStr的值
String inputStr=request.getParameter("txt");
//再进行utf-8编码
byte d[] =inputStr.getBytes("utf-8");
inputStr=new String(d);
//最后输出inputStr
out.println(inputStr);
%>
<br>
获取文本框2的值 :
<% //先获取inputStr的值
String inputStr2=request.getParameter("txt2");
//调用Change.changeStr()方法进行utf-8编码
String newInput2=Change.changeStr(inputStr2);
//最后输出inputStr
out.println(newInput2);
%>
<br>
获取下拉框的值 :
<% //先获取inputStr的值
String select=Change.changeStr(request.getParameter("select"));
out.println(select);
%>
<br>
获取radiobutton的值 :
<% //先获取inputStr的值
String rad=Change.changeStr(request.getParameter("rad1"));
out.println(rad);
%>
<br>
获取按钮的值:
<%
String button=request.getParameter("submit");
byte c[] =button.getBytes("utf-8");
button=new String(c);
out.println(button);
%>
<br>
<hr>
获取表单传递方法 :<% out.println(request.getMethod()); %>
<br>
获取网站根目录: <% out.println(path); %>
<br>
获取网站访问地址: <% out.println(basePath); %>
<br>
获取通信协议: <% out.println(request.getProtocol()); %>
<br>
获取请求方式:<% out.println(request.getScheme()); %>
<br>
获取服务器名称: <%=request.getServerName() %>
<br>
获取通信端口: <%=request.getServerPort() %>
<br>
使用者IP:<%=request.getRemoteAddr() %>
<br>
主机地址:<%=request.getRemoteHost() %>
<br>
<%=request.getServletPath() %>
<br>
<%=request.getCharacterEncoding() %>
<br>
<%=request.getParameterNames() %>
<br>
<%=request.getParameterValues("txt") %>
<br>
<%=request.getRequestURI() %>
<br>
<%=request.getQueryString() %>
<br>
<%=request.getContentLength() %>
</body>
</html>