Servlet--传参和接参

OK,现在基本的Servlet接口和常用类都整理的差不多的,本篇博客开始整理Servlet和页面的交互。
1,接参

以下几个常用的方法:
getParameter public String getParameter(String name);
以一个String返回指定的参数的值,如果这个参数不存在返回空值。例如,在一个HTTP Servlet中,这个方法会返回一个指定的查询语句产生的参数的值或一个被提交的表单中的参数值。如果一个参数名对应着几个参数值,这个方法只能返回通过getParameterValues方法返回的数组中的第一个值。
特别要注意:如果这个参数有(或者可能有)多个值,你只能使用getParameterValues方法。

getParameterNames public Enumeration getParameterNames();
返回所有参数名的String对象列表,如果没有输入参数,该方法返回一个空值。

getParameterValues public String[] getParameterValues(String name);
通过一个String对象的数组返回指定参数的值,如果这个参数不存在,该方法返回一个空值。


另外还有一个就是getParameterMap()方法,但是我自己去翻API,竟然没有找见,难道是API过时了?先不管,这个方法返回的所有的参数和参数的值,被封装成了一个map返回。

<%@ page language="java" import="java.util.*" pageEncoding="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>Servlet表单提交</title>


</head>

<body>
	<form action="<%=path%>/LinkinServlet" method="POST">
		姓名:<input type="text" name="userName" />
		<br />
		年龄:<input type="text" name="passWord" />
		<br />
		学校:<input type="checkbox" name="school" value="学校1" />学校1
		<input type="checkbox" name="school" value="学校2" />学校2
		<input type="checkbox" name="school" value="学校3" />学校3
		<input type="checkbox" name="school" value="学校4" />学校4
		<input type="submit" value="提交" name="tijiao"/>
	
	
	</form>
</body>
</html>
package linkin;

import java.io.IOException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LinkinServlet extends HttpServlet
{
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		req.setCharacterEncoding("UTF-8");
		String userName = req.getParameter("userName");
		System.out.println(userName);
		String[] schools = req.getParameterValues("school");
		for (String school : schools)
		{
			System.out.println(school);
		}

		Enumeration enumeration = req.getParameterNames();
		while (enumeration.hasMoreElements())
		{
			String name = (String) enumeration.nextElement();
			System.out.println(name + ":" + req.getParameter(name));
		}

		Map<String, String[]> map = req.getParameterMap();
		for (String key : map.keySet())
		{
			System.out.println(Arrays.asList(map.get(key)));
		}
		for (Map.Entry<String, String[]> entry : map.entrySet())
		{
			System.out.println(entry.getKey() + ":" + Arrays.asList(entry.getValue()));
		}
		System.out.println(req.getMethod());
		System.out.println(req.getQueryString());
		Enumeration enumeration1 = req.getHeaderNames();
		while (enumeration1.hasMoreElements())
		{
			System.out.println(enumeration1.nextElement() + ":" + req.getHeader((String) enumeration1.nextElement()));
		}
		/*
		 * host:keep-alive
		 * content-length:max-age=0
		 * accept:http://localhost:8080
		 * user-agent:application/x-www-form-urlencoded
		 * referer:gzip, deflate
		 * accept-language:JSESSIONID=4214F4CE399E02730C3219CF0BD83623
		 */
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		this.doGet(req, resp);
	}

}
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	
	
	<servlet>
		<servlet-name>LinkinServlet</servlet-name>
		<servlet-class>linkin.LinkinServlet</servlet-class>
		<init-param>
			<param-name>name</param-name>
		<param-value>LinkinPark...</param-value>
		</init-param>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>LinkinServlet</servlet-name>
		<url-pattern>/LinkinServlet</url-pattern>
	</servlet-mapping>

</web-app>

2,传参

setAttribute public void setAttribute(String name, Object object);
这个方法在请求中添加一个属性,这个属性可以被其他可以访问这个请求对象的对象(例如一个嵌套的Servlet)使用。

传参一般用setAttribute() 方法,不管是request,还是session,还是application都可以用这个方法将值放进去,不同的只是放入的范围不同而已。

package linkin;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LinkinServlet extends HttpServlet
{
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		req.setCharacterEncoding("UTF-8");
		String userName = req.getParameter("userName");
		String passWord = req.getParameter("passWord");
		req.setAttribute("userName", userName);
		req.getSession().setAttribute("passWord", passWord);
		req.getRequestDispatcher("/Linkin1.jsp").forward(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		this.doGet(req, resp);
	}

}
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	
	
	<servlet>
		<servlet-name>LinkinServlet</servlet-name>
		<servlet-class>linkin.LinkinServlet</servlet-class>
		<init-param>
			<param-name>name</param-name>
		<param-value>LinkinPark...</param-value>
		</init-param>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>LinkinServlet</servlet-name>
		<url-pattern>/LinkinServlet</url-pattern>
	</servlet-mapping>

</web-app>
<%@ page language="java" import="java.util.*" pageEncoding="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>Servlet表单提交</title>


</head>

<body>
	<form action="<%=path%>/LinkinServlet" method="POST">
		姓名:<input type="text" name="userName" />
		<br />
		年龄:<input type="text" name="passWord" />
		<br />
		学校:<input type="checkbox" name="school" value="学校1" />学校1
		<input type="checkbox" name="school" value="学校2" />学校2
		<input type="checkbox" name="school" value="学校3" />学校3
		<input type="checkbox" name="school" value="学校4" />学校4
		<input type="submit" value="提交" name="tijiao"/>
	
	
	</form>
</body>
</html>

<%@ page language="java" import="java.util.*" pageEncoding="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>Servlet显示数据</title>


</head>

<body>
	OK,成功跳转到页面。
	姓名:<%=request.getAttribute("userName") %>;
	年龄:<%=session.getAttribute("passWord") %>
</body>
</html>







原文地址:https://www.cnblogs.com/LinkinPark/p/5233007.html