欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

WEB应用开发设计实验报告三

程序员文章站 2022-05-05 16:34:44
...

一.实验课题
请求转发

二. 实验目的
1.掌握ServletConfig对象的使用。

三. 实验内容
在Servlet中,如果当前Web资源不想处理请求时,可以通过forward()方法将当前请求传递给其它的Web资源进行处理,这种方式称为请求转发。本案例将模拟演示在一个servlet查询到用户id,然后请求转发到另外一个servlet,再通过该用户id来获取用户信息进行响应。

四.实验步骤
1)创建ReqServlet3和ReqServlet4两个servlet类,
2)在ReqServlet3中,将获取的用户id放在request域中,并请求转发到另一个ReqServlet4中,
3)在ReqServlet4中,获取用户id,然后显示该用户信息,并响应,
4)在web.xml中配置初始化参数和类的映射
5)通过浏览器访问ReqServlet3,展示查询信息

在以下表格中给出实验内容的相应代码。
ReqServlet3:

package servlet_test;

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;

/**
 * Servlet implementation class ReqServlet3
 */
public class ReqServlet3 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset = utf-8");
		//获取PrintWriter对象用于输出信息
		PrintWriter out = response.getWriter();
		//获取request请求对象中保存的数据
		String userNumber = (String) request.getAttribute("userNumber");
		String userName = (String) request.getAttribute("userName");
		String userAge = (String) request.getAttribute("userAge");
		
		if(userNumber !=null) {
			out.println("查询编号"+ userNumber+"的顾客为: "+"<br>");
			out.println("用户名: "+ userName+"<br>");
			out.println("用户年龄: "+ userAge+"<br>");
		}
	}

	/**
	 * @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);
	}

}

ReqServlet4:

package servlet_test;

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 ReqServlet4
 */
public class ReqServlet4 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset = utf-8");
		//将数据储存到request对象中
		request.setAttribute("userNumber", "001");
		request.setAttribute("userName", "王德发");
		request.setAttribute("userAge", "38");
		
		RequestDispatcher dispatcher = request.getRequestDispatcher("/ReqServlet3");
		dispatcher.forward(request, response);
	}

	/**
	 * @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);
	}

}

五. 实验结果(截图)
此处对上述所有实验内容最终呈现的网页或操作界面进行截图展示
WEB应用开发设计实验报告三
六.心得与体会(不少于100字)
一点一点根据案例改:
用PrintWriter来输出信息,用request.setAttribute来保存数据,用RequestDispatcher(调度员)方法来创建一个dispatcher对象储存调度路径,然后dispatcher.forward来传递B的处理答案(功效等同于电线,不加这个传递不过去);
在B的Servlet文件里用request.getAttribute(属性)来获取request里的数据,然后处理完通过forward传给A。最后网址写A的,可以查看B的运算结果。
整体类似于A把题给B做然后A抄完了给老师看。

相关标签: servlet java