【任务目标】

在实际的项目开发过程中,Servlet中的service()方法由Servlet容器调用,所以一个Servlet的对象无法调用另一个Servlet的方法。但是在实际项目中,客户端请求做出的响应可能比较复杂,例如用户登录,需要多个Servlet来完成,这就需要使用请求转发技术了。

代码下载:https://url47.ctfile.com/f/64055047-1497324283-256305?p=1232

访问密码1232

一、编写中转页面

右击web文件夹,选择New—>JSP/JSPX,名字为forward.jsp

参考代码如下:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>中转页</title>
</head>
<body>
<jsp:forward page="login.jsp"/>
</body>
</html>
二、编写用户登录页面

右击src,选择New—>JSP/JSPX,名字为login.jsp

代码参考如下:


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户登录</title>
</head>
<body>
    <form name="form1" method="post" action="">
        用户名:<input name="name" type="text" id="name" style="width: 200px"><br><br>
        密&nbsp;&nbsp;码: <input name="pwd" type="password" id="pwd" style="width: 200px"><br><br>
        <input type="submit" name="Submit" value="提交">
    </form>
</body>
</html>
三、运行

运行tomcat,在浏览器里访问下面的地址:

http://localhost:8080/chapter06/forward.jsp

运行效果如下:

分类: java