课外天地 李树青学习天地JavaEE网站开发课件 → 网页转发的常见方法


  共有15196人关注过本帖树形打印复制链接

主题:网页转发的常见方法

帅哥哟,离线,有人找我吗?
admin
  1楼 博客 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 管理员
等级:管理员 帖子:1939 积分:26594 威望:0 精华:34 注册:2003/12/30 16:34:32
网页转发的常见方法  发帖心情 Post By:2010/4/20 21:26:37 [只看该作者]

除了使用超链标签<a>以外,还有很多常见的网页转发方法

1)使用submit提交表单
default.html文件为:
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<form action=result.jsp><input type=text name=inputvalue></input><input
        type=submit value=提交></form>
</body>
</html>

result.jsp文件为:
<%@page c%>

<html>
<head>
</head>
<body>
<%=request.getParameter("inputvalue")%>
</body>
</html>

2)使用一般按钮结合脚本提交表单
default.html文件为:
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<form action=result.jsp><input type=text name=inputvalue></input><input
        type="button" value="提交" ></form>
</body>
</html>

<script language=javascript>
//客户端验证函数
function check()
{
        document.forms[0].submit();
}
</script>

3)使用一般按钮结合脚本转发网页
default.html文件为:
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<form action=result.jsp><input type=text name=inputvalue></input><input
        type="button" value="提交"
        ></form>
</body>
</html>

此时result.jsp不能显示正确内容,因为不是表单提交

4)使用servlet(最好、最灵活的方式)
default.html文件为:
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<form action=redirect><input type=text name=inputvalue></input><input
        type="submit" value="提交"></form>
</body>
</html>

名称为Redirect的Servlet,映射名称为redirect
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;

public class Redirect extends HttpServlet {
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                        throws IOException, ServletException {
                doGet(request, response);
        }

        public void doGet(HttpServletRequest request, HttpServletResponse response)
                        throws IOException, ServletException {
                if (!request.getParameter("inputvalue").equals("null")) {
                        RequestDispatcher disp = request.getRequestDispatcher("result.jsp");
                        disp.forward(request, response);
                }
        }
}


 

[此贴子已经被作者于2010-12-12 18:11:57编辑过]

 回到顶部