飞道的博客

java中request.RequestDispatcher(String name).forward(request,response)接收不到值的问题

234人阅读  评论(0)

java中request.RequestDispatcher(String name).forward(request,response)接收不到值的问题

#首先 说问题

第一次使用request.RequestDispatcher(String name).forward(request,response),在使用过程中遇到了一个棘手的问题:编程过程中语法没有问题,逻辑上也不存在问题,但是在测试过程中跳转之后的页面(third)却总是获取不到上一个页面的request.setAttribuate(String name,Object o)这个值

在此 附上测试代码(分三个页面 html second third)third中我将更改后的代码单独写在了third之后。

html页面:


```java
package com.hjk.temp;

public class html extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		
		out.print("<form action = '/ServletPro/servlet/second' method = 'post'>");
		out.print("姓名:<input type = 'text' name = 'name'/></br>");
		out.print("密码:<input type = 'text' name = 'psd'/></br>");
		out.print("<input type = 'submit' name = '提交' value = 'tijiao'>");
	
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doGet(request,response);
		
	}

}

second页面:

```java
package com.hjk.temp;

import java.io.IOException;
import java.io.PrintWriter;

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

public class second extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		//接受用户输入的信息
		String name = request.getParameter("name");
		String psd  = request.getParameter("psd");
		
		//将接收到的信息传递给下一个页面(此处仅将name值传递)
		
		request.setAttribute("name", "get"+name);
		request.getRequestDispatcher("/third").forward(request, response);
		
		
		
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
				
			response.setContentType("html/text;charset=utf-8");
			PrintWriter out = response.getWriter();
				//接受用户输入的信息
				String name = request.getParameter("name");
				String psd  = request.getParameter("psd");
				
				//将接收到的信息传递给下一个页面(此处仅将name值传递)
				
				request.setAttribute("name", "post"+name);
				request.getRequestDispatcher("/servlet/third").forward(request, response);
				
		
	}

}

third页面:

package com.hjk.temp;

import java.io.IOException;
import java.io.PrintWriter;

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

public class third extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		
		//接受从第二个servlet传入的值
		String name = (String) request.getAttribute("name");
		
		//输出内容
		out.print(name);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		
		
		
	}

}

//接受从第二个servlet传入的值
		String name = (String) request.getAttribute("name");
		
		//输出内容
		out.print(name);
	//将此代码放入third中的post方法中即可

问题原由

在html中我的表单提交方式为post提交,在将输入的数据传递到second页面中后,接受是用post接受的,到此没有问题,在从second页面往third页面传递时,出现了问题。大家都知道 这个方法叫做转向,就是说只是在服务器内部跳转到另一个页面,其中的信息提交方式也会一并转过去,意思就是说,从second到third的提交方式还是post方法,所以在third中理应用doPost()方式接收信息。这就是问题所在。

解决方法:

在third中使用post方法接受传递过来的信息

对于你来说:
要设置转向的信息接收方的接收方式,如果之前是post 则就用doPost接收,反之用doGet接收,保持与转向前的方式相同。

后记

代码是个严谨的东西!!!


转载:https://blog.csdn.net/weixin_43498703/article/details/105299562
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场