jsp页面跳转和请求的各种方法

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>首先请求显示的页面</title>
</head>
<body>
    <hr>
    <!--获取项目的应用名-->
    ${pageContext.request.contextPath}
    <hr>
    <!--全部使用的是绝对路径-->
    <!--此处是利用a标签进行客户端页面的跳转-->
    <a href="${pageContext.request.contextPath}/jsppage/requestpage.jsp">
        <img src="${pageContext.request.contextPath}/imgs/submit.jpg"
        style="cursor: pointer; width: 50px; height: 50px">
    </a>
    <!--此处是利用a标签进行服务器端的请求,参数的传递在链接后面用?连接-->
    <a
        href="${pageContext.request.contextPath}/jsppage/requestpage.ht?name=merida">
        <img src="${pageContext.request.contextPath}/imgs/submit.jpg"
        style="cursor: pointer; width: 50px; height: 50px">
    </a>
    <hr>
    <!--此处是利用form标签中的action属性来向服务器发送请求-->
    <form
        action="${pageContext.request.contextPath}/jsppage/requestpage.ht">
        name:<input type="text" name="name"> <input type="submit"
            value="提交">
    </form>
    <hr>
    <h3>需要注意的是,获取到form对象,绑定一个submit()方法来实现表单的提交.</h3>
    <!--此处是将a标签的href属性设置为#,给a标签绑定一个鼠标的点击事件,点击事件中根据form表单的id属性获取form对象,给该对象绑定一个submit()。-->
    <form id="subform"
        action="${pageContext.request.contextPath}/jsppage/requestpage.ht">
        name:<input type="text" name="name"> <a href="#"
            onclick="document.getElementById(‘subform‘).submit();return false">
            <img src="${pageContext.request.contextPath}/imgs/提交.png"
            style="cursor: pointer;">
        </a>
    </form>
    <hr>
    <form id="subform1"
        action="${pageContext.request.contextPath}/jsppage/requestpage.ht">
        name:<input type="text" name="name">
        <!--此处是将a标签的作用嫁接给img标签,同样可以实现表单的提交-->
        <img src="${pageContext.request.contextPath}/imgs/提交.png"
            style="cursor: pointer;"
            onclick="document.getElementById(‘subform1‘).submit();return false">
        </a>
    </form>
    <hr>
    <!-- 请求转发:request.getRequestDispatcher(URL地址).forward(request, response)
        此处是利用了请求的转发,来请求服务器
    -->
     <%-- <%
    request.setAttribute("name", "GIANT");
    /* request.getRequestDispatcher("${pageContext.request.contextPath}/jsppage.ht").forward(request, response); */
    %> --%> <hr>
    <%-- <%
    /* 此处是利用转发来在客户端跳转页面 */
    request.getRequestDispatcher("/jsppage/requestpage.jsp").forward(request, response);
    %> --%>
    <%-- 在JSP页面中,可以使用<jsp:forward>标签来转发请求 --%>
    <%-- <jsp:forward page="/jsppage/requestpage.jsp"> --%>
</body>
</html>
package ResponseServlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ResponseServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)    throws ServletException, IOException {
        //获取项目的应用名
        String  path="\\"+request.getServerName();
        if(!(request.getParameter("name")==null)){
            String name=request.getParameter("name");
            response.getWriter().println("从request参数中获取的值"+name);
            response.sendRedirect(path+"/jsppage/Redirectpage.jsp");
        }else{
            String nameString=(String)request.getAttribute("name");
            response.getWriter().println("从request作用域中get出的值"+nameString);
            response.sendRedirect(path+"/jsppage/Redirectpage.jsp");
        }
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>requestmethod</display-name>
  <welcome-file-list>
    <welcome-file>/jsppage/index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
  <servlet-name>pagerequest</servlet-name>
  <servlet-class>ResponseServlet.ResponseServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>pagerequest</servlet-name>
  <url-pattern>*.ht</url-pattern>
  </servlet-mapping>
</web-app>
时间: 2024-08-06 07:56:41

jsp页面跳转和请求的各种方法的相关文章

Jsp页面跳转和js控制页面跳转的几种方法

Jsp 页面跳转的几种方法 1. RequestDispatcher.forward() 在服务器端起作用,当使用forward()时,Servlet engine传递HTTP请求从当前的Servlet或者是JSP到另外的一个Servlet.JSP 或普通HTML文件,也即你的form提交至a.jsp,在a.jsp用到了forward()重定向至b.jsp,此时form提交的所有信息在 b.jsp都可以获得,参数自动传递. 但forward()无法重定向至有frame的jsp文件,可以重定向至有

JSP页面跳转Servlet

JSP页面跳转Servlet 项目结构: 2. JSP页面中 1 <% 2 String path = request.getContextPath(); //上下文路径 3 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 4 %> 1 <!--使用相对路径访问Hel

jsp页面跳转后台代码的方式总结~

jsp页面跳到后台代码,有如下几种方式: action方式: jquery方式,代码如下: function regCust(){ $('#containerFRM').form( 'submit', { "url" : "${webAppUrl}/cust/registeredUser.html", onSubmit : function() { //显示遮罩 $.jBox.tip("正在提交--", 'loading'); }, succes

jsp页面跳转

window.history.back(-1) window.history.back(); window.history.forward() window.history.go(-1) javascript:history.go(-1)和javascript:history.back(-1) go(-1): 返回上一页, 原页面表单中的内容会丢失; back(-1): 返回上一页, 原页表表单中的内容会保留. go(-1): 返回上一页, 原页面表单中的内容会丢失; back(-1): 返回上

JSP页面间传递参数的5种方法

JSP页面间传递参数是经常需要使用到的功能,有时还需要多个JSP页面间传递参数.下面介绍一下实现的方法. (1)直接在URL请求后添加 如:< a href="thexuan.jsp?action=transparams&detail=directe">直接传递参数< /a> 特别的在使用response.sendRedirect做页面转向的时候,也可以用如下代码: response.sendRedirect("thexuan.jsp?acti

jsp页面保存到数据库有乱码解决方法

第一种: 在页面前加上 <%@ page language="java" contentType="text/html;charset=gbk" errorPage=""%> <%request.setCharacterEncoding("GBK");%> 第一行说明你的页面用的是中文编码 第二行声明你的页面传值也用中文编码 第二种 tomcat4.x支持中文传码,但5.x不支持,如果用5.0以上的版本

实现jsp页面两级列表的显示(Map 传值到Jsp 页面;Map去key 和 value 的方法; 实例记载,备不时之需。

Action: List.vm 页面效果: 实现jsp页面两级列表的显示(Map 传值到Jsp 页面;Map去key 和 value 的方法: 实例记载,备不时之需.

五种JSP页面跳转方法详解

1. RequestDispatcher.forward() 是在服务器端起作用,当使用forward()时,Servlet engine传递HTTP请求从当前的Servlet or JSP到另外一个Servlet,JSP 或普通HTML文件,也即你的form提交至a.jsp,在a.jsp用到了forward()重定向至b.jsp,此时form提交的所有信息在 b.jsp都可以获得,参数自动传递. 但forward()无法重定向至有frame的jsp文件,可以重定向至有frame的html文件,

jsp页面跳转方式

1. RequestDispatcher.forward() 是在服务器端起作用,当使用forward()时,Servlet engine传递HTTP请求从当前的Servlet or JSP到另外一个Servlet,JSP 或普通HTML文件,也即你的form提交至a.jsp,在a.jsp用到了forward()重定向至b.jsp,此时form提交的所有信息在 b.jsp都可以获得,参数自动传递. 但forward()无法重定向至有frame的jsp文件,可以重定向至有frame的html文件,