-- 作者:admin
-- 发布时间: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编辑过]
|