如题所述
具体方式和区别如下:
1、第一种方法:通过HttpServletResponse的API直接输出
步骤:
控制层:controller类的编写
@Controller
public class RequestController{
@RequestMapping("/request") //映射地址注入
public void handleRequest(HttpServletRequest rq, HttpServletResponse rp) throws Exception {
rp.getWriter().println("request");
}
2、web.xml文件的配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
3、第二种方法:使用HttpServletResponse 类进行重定向跳转视图
@RequestMapping("/Response")
public void handleRequest(HttpServletRequest rq, HttpServletResponse rp) throws Exception {
rp.sendRedirect(url); //跳转的指定页面。
}
}
4、第三种:使用HttpServletRequest 转发
@RequestMapping("/request")
public void handleRequest(HttpServletRequest rq, HttpServletResponse rp) throws Exception {
req.setAttribute("属性名","属性值"); //赋值
req.getRequestDispatcher(url).forward(rq,rp); //跳转
}