返回值类型 | 方法 | 功能描述 |
---|---|---|
void | forward(ServletRequest request,ServletResponse response) | 用于将请求转发给另一个 Web 资源。该方法必须在响应提交给客户端之前被调用,否则将抛出 IllegalStateException 异常 |
void | include(ServletRequest request,ServletResponse response) | 用于将其他的资源作为当前响应内容包含进来 |
返回值类型 | 方法 | 描述 |
---|---|---|
void | setAttribute(String name, Object o) | 将 Java 对象与属性名绑定,并将它作为一个属性存放到 request 对象中。参数 name 为属性名,参数 object 为属性值。 |
Object | getAttribute(String name) | 根据属性名 name,返回 request 中对应的属性值。 |
void | removeAttribute(String name) | 用于移除 request 对象中指定的属性。 |
Enumeration | getAttributeNames() | 用于返回 request 对象中的所有属性名的枚举集合。 |
package net.biancheng.www; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * * @author 编程帮 www.lmcjl.com *请求转发 */ @WebServlet("/DispatcherServlet") public class DispatcherServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置向页面输出内容格式 response.setContentType("text/html;charset=UTF-8"); PrintWriter writer = response.getWriter(); // 尝试在请求转发前向response缓冲区写入内容,最后在页面查看是否展示 writer.write("<h1>这是转发前在响应信息内的内容!</h1>"); // 向reuqest域对象中添加属性,传递给下一个web资源 request.setAttribute("webName", "C语言中文网"); request.setAttribute("url", "www.lmcjl.com"); request.setAttribute("welcome", "C语言中文网,欢迎你"); // 转发 request.getRequestDispatcher("/DoServlet").forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
package net.biancheng.www; import java.io.IOException; import java.io.PrintWriter; import java.util.Arrays; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * * @author 编程帮 www.lmcjl.com * 请求转发 * */ @WebServlet("/DoServlet") public class DoServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置向页面输出内容格式 response.setContentType("text/html;charset=UTF-8"); PrintWriter writer = response.getWriter(); String webName = (String) request.getAttribute("webName"); String url = (String) request.getAttribute("url"); String welcome = (String) request.getAttribute("welcome"); if (webName != null) { writer.write("<h3>" + webName + "</h3>"); } if (url != null) { writer.write("<h3>" + url + "</h3>"); } if (welcome != null) { writer.write("<h3>" + welcome + "</h3>"); } String username = request.getParameter("username"); // 获取密码 String password = request.getParameter("password"); // 获取性别 String sex = request.getParameter("sex"); // 获取城市 String city = request.getParameter("city"); // 获取使用语言返回是String数组 String[] languages = request.getParameterValues("language"); writer.write("用户名:" + username + "<br/>" + "密码:" + password + "<br/>" + "性别:" + sex + "<br/>" + "城市:" + city + "<br/>" + "使用过的语言:" + Arrays.toString(languages) + "<br/>" ); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="/httpServletRequestDemo/DispatcherServlet" method="GET"> <table border="1" width="50%"> <tr> <td colspan="2" align="center">编程帮wwww.lmcjl.com</td> </tr> <tr> <td>输入姓名</td> <td><input type="text" name="username" /></td> </tr> <tr> <td>输入密码</td> <td><input type="password" name="password" /></td> </tr> <tr> <td>选择性别</td> <td><input type="radio" name="sex" value="男" />男 <input type="radio" name="sex" value="女" />女</td> </tr> <tr> <td>选择使用的语言</td> <td><input type="checkbox" name="language" value="JAVA" />JAVA <input type="checkbox" name="language" value="C语言" />C语言 <input type="checkbox" name="language" value="PHP" />PHP <input type="checkbox" name="language" value="Python" />Python</td> </tr> <tr> <td>选择城市</td> <td><select name="city"> <option value="none">--请选择--</option> <option value="北京">北京</option> <option value="北京">上海</option> <option value="广州">广州</option> </select></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="提交" /> </td> </tr> </table> </form> </body> </html>
本文链接:http://task.lmcjl.com/news/13490.html