jsp前后端交互以及请求转发和请求包含

request对象 作用: 获得请求参数:
请求方式:

API: 获得参数:

  • 前后端交互 涉及到前后端交互的都很重要

getParameter(“name”); 能够获得前段页面提交的参数:

Returns the value of a request parameter as a String, or null if the parameter does not exist.

getParameterValues(java.lang.String name)

Returns an array of String objects containing all of the values the given request parameter has,
or null if the parameter does not exist.
java.util.Map getParameterMap()
Returns a java.util.Map of the parameters of this request.

jar包: commons-beanUtils.jar 依赖于
commons-logging.jar
jar包的依赖!

一般的通过对象获取数据

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;

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

/**
 * Servlet implementation class AServlet
 *  使用get 提交的方式: 完成参数的获取: 
 *  
 *  getParameter(); 
 */
public class AServlet extends HttpServlet {
	

	
	public void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
	    //核心的代码: 
		
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		PrintWriter out = response.getWriter();
		
		out.println(""+username+"");
		out.println(""+password+"");
		out.close(); 
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		//doGet(request, response);
		
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		PrintWriter out = response.getWriter();
		
		out.println(""+username+"");
		out.println(""+password+"");
		
//		=======================================
//		获得同一个参数的名称对应多个值的情况: 
		String[] values = request.getParameterValues("hobby");
		System.out.println(Arrays.toString(values));
		
		for (int i = 0; i < values.length; i++) {
			String val = values[i]+",";
			System.out.print(val);
		}
		
//		=======================================
		out.close(); 
	
	}

}

用javabean封装前端传过来的数据

import java.io.Serializable;
import java.util.Arrays;

/*
 * javaBean 
 * 满足: 
 *  必须属性: set/get后边的内容称之为属性: 
 *  必须提供无参数的构造器: 
 *  最好: 提供实现Serializable 接口: 
 */
public class User implements Serializable {
	
	
	private static final long serialVersionUID = 1L;
	
	private String username; 
	private String password; 
	private String hobbies[];
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String[] getHobbies() {
		return hobbies;
	}
	public void setHobbies(String[] hobbies) {
		this.hobbies = hobbies;
	}
	/**
	 * @param username
	 * @param password
	 * @param hobbies
	 */
	public User(String username, String password, String[] hobbies) {
		super();
		this.username = username;
		this.password = password;
		this.hobbies = hobbies;
	}
	/**
	 * 
	 */
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	@Override
	public String toString() {
		return "User [username=" + username + ", password=" + password + ", hobbies=" + Arrays.toString(hobbies) + "]";
	} 
	
	
	
}

使用jar包封装数据

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

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

import org.apache.commons.beanutils.BeanUtils;

import com.yidongxueyuan.domain.User;

/**
 * Servlet implementation class BServlet、
 * 将前段页面提交的参数 封装到map集合当中: 
 */
public class BServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
   

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//將獲得的的数据:
//		获得前段的参数封装map 集合当中: 
		Map map = request.getParameterMap();
		System.out.println(map);
		
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		doGet(request, response);
		
		/*
		 * 提交的参数: 封装javaBean  对象当中: 
		 */
//		获得前段页面的参数: 
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String[] values = request.getParameterValues("hobby");
		
		//准备一个javaBean对象: 
		User user = new User(); 
		user.setUsername(username);
		user.setPassword(password);
		user.setHobbies(values);
		
		
		System.out.println(user);
		System.out.println("======");
		doPost02(request,response); 
	}
	
	//使用jar包 : 快速的封装数据: 
	/*
	 * 使用jar包;快速的封装数据: 
	 * 对象当中的属性和 前段提供的属性必须一致: 
	 */
	public void doPost02(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    try {
	    	   Map map = request.getParameterMap();	
	    	   //Apache组织提供了一个jar : commons组件: 
	    	   User user = new User();
			   BeanUtils.populate(user, map);
			   System.out.println(user);
		} catch (IllegalAccessException | InvocationTargetException e) {
			e.printStackTrace();
		}
	}

}`在这里插入代码片`

  • 请求转发和请求包含:

重定向的原理:
重定向的第二次: 一定是get :
请求转发和请求包含原理:
(1)一次请求,一次响应。
(2)地址栏不会发生变化。 显示的是第一次请求的地址。
(3)服务器端的行为。
(4)只能转发到站内资源。
(5)请求转发对AServlet和BServlet的请求方法是相同的,即要么都是GET,要么都是POST,因为请求转发是一个请求;

代码的实现:
CServlet: 负责转发的Servlet
DServlet: 被转发过来的Servlet:

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

/**
 * Servlet implementation class CServlet
 * 演示: 请求转发: 
 */
public class CServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//负责转发的Servlet: 
		
		//获得一个请求分配器: 
//		RequestDispatcher dis = request.getRequestDispatcher("DServlet");
//		
//		//转发到DServlet: DServlet 和CServlet 共享的是同一个request对象。 
//		dis.forward(request, response);
		
		
		//解决乱码的相应头信息: 
		response.setHeader("content-type", "text/html;charset=utf-8");//告知客户端浏览器 使用utf-8解析: 
		response.setCharacterEncoding("utf-8");// 设置响应输出流的编码是utf-8
		
		response.getWriter().println("this is CServlet....");
		
		//完成了请求转发:
		request.getRequestDispatcher("/DServlet").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	
	}

}

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

/**
 * 被转发过来的Servlet: 
 */
public class DServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//响应内容: 
		
		response.getWriter().println("转发过来的DServlet......xxx");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

请求包含代码实现:

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

/**
 * 负责请求包含的Servlet : 
 */
public class EServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("FServlet").include(request, response);
		//解决了响应乱码的问题: 
	/*	response.setHeader("content-type", "text/html;charset=utf-8");//告知客户端浏览器 使用utf-8解析: 
		response.setCharacterEncoding("utf-8");// 设置响应输出流的编码是utf-8
*/	   
		//设置头: 
		response.setHeader("xxx", "mrguo");
		response.getWriter().println("负责包含的servlet .....E");;
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

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

/**
 * Servlet implementation class FServlet
 * 这是被转发过来的FServlet: 
 */
public class FServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//解决了响应乱码的问题: 
				response.setHeader("content-type", "text/html;charset=utf-8");//告知客户端浏览器 使用utf-8解析: 
				response.setCharacterEncoding("utf-8");// 设置响应输出流的编码是utf-8
		response.getWriter().append("Served at: ").append(request.getContextPath());
		response.getWriter().println("这是被请求包含过来的Servlet.......F");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

- 请求转发和请求包含之间的区别:

相同点:原理相同
a: 转发:负责转发的servlet 留头不留体: 设置的响应头生效, 设置的响应体 不生效。
在上面的代码中c servlet的请求体没有响应
b: 包含:负责包含的Servlet 留头又留体:

你可能感兴趣的:(java)