码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2015-10-30 18:22:45      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

<%@ 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>

 

 

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

标签:

原文地址:http://www.cnblogs.com/cuitrek/p/4923723.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!