Servlet跳转到jsp页面的几种方法

servlet文件

  1. package com.mi.servlet;  
  2.   
  3. import java.io.IOException;  
  4.   
  5.   
  6. import java.io.PrintWriter;  
  7.   
  8. import javax.servlet.ServletException;  
  9. import javax.servlet.http.HttpServlet;  
  10. import javax.servlet.http.HttpServletRequest;  
  11. import javax.servlet.http.HttpServletResponse;  
  12. import com.mi.beans.*;  
  13. import java.sql.*;  
  14. import java.util.ArrayList;  
  15. public class List_book extends HttpServlet {  
  16.   
  17.       
  18.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  19.             throws ServletException, IOException {  
  20.         Connection conn = null;  
  21.         Statement sta = null;  
  22.         ResultSet res = null;  
  23.         conn = DBConn.getConn();  
  24.         //声明一个ArrayList.用来存放Book类中的数据  
  25.         ArrayList<Book> list = new ArrayList<Book>();  
  26.         try {  
  27.             sta = conn.createStatement();  
  28.             String sql = “select * from book;”;  
  29.             res = sta.executeQuery(sql);  
  30.             while (res.next()) {  
  31.                 //建立了一个实体类,用来存放从数据库中拿到的数据  
  32.                 Book book = new Book();  
  33.                 book.setName(res.getString(“name”));  
  34.                 book.setAuthor(res.getString(“author”));  
  35.                 book.setPrice(res.getString(“price”));  
  36.                 book.setCountry(res.getString(“country”));  
  37.                 list.add(book);  
  38.             }  
  39.             //将list数据打包  
  40.             request.setAttribute(“list”, list);  
  41.               
  42.         } catch (SQLException e) {  
  43.             e.printStackTrace();  
  44.         }  
  45.           
  46.         //将list数据发送到.jap文件中  
  47.         request.getRequestDispatcher(“ListBook.jsp”).forward(request, response);  
  48.     }  
  49.   
  50.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  51.             throws ServletException, IOException {  
  52.         doGet(request, response);  
  53.     }  
  54.   
  55. }  
package com.mi.servlet;

import java.io.IOException;


import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mi.beans.*;
import java.sql.*;
import java.util.ArrayList;
public class List_book extends HttpServlet {


    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Connection conn = null;
        Statement sta = null;
        ResultSet res = null;
        conn = DBConn.getConn();
        //声明一个ArrayList.用来存放Book类中的数据
        ArrayList<Book> list = new ArrayList<Book>();
        try {
            sta = conn.createStatement();
            String sql = "select * from book;";
            res = sta.executeQuery(sql);
            while (res.next()) {
                //建立了一个实体类,用来存放从数据库中拿到的数据
                Book book = new Book();
                book.setName(res.getString("name"));
                book.setAuthor(res.getString("author"));
                book.setPrice(res.getString("price"));
                book.setCountry(res.getString("country"));
                list.add(book);
            }
            //将list数据打包
            request.setAttribute("list", list);

        } catch (SQLException e) {
            e.printStackTrace();
        }

        //将list数据发送到.jap文件中
        request.getRequestDispatcher("ListBook.jsp").forward(request, response);
    }

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

}


jsp文件

  1. <strong><span style=“font-size:18px;”><%@page import=“com.mi.beans.Book”%>  
  2. <%@ page language=“java” import=“java.util.*” pageEncoding=“utf-8”%>  
  3. <%  
  4. String path = request.getContextPath();  
  5. String basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;  
  6. %>  
  7.   
  8. <!DOCTYPE HTML PUBLIC ”-//W3C//DTD HTML 4.01 Transitional//EN”>  
  9. <html>  
  10.   <head>  
  11.     <base href=“<%=basePath%>”>  
  12.       
  13.     <title>My JSP ‘ListBook.jsp’ starting page</title>  
  14.       
  15.     <meta http-equiv=“pragma” content=“no-cache”>  
  16.     <meta http-equiv=“cache-control” content=“no-cache”>  
  17.     <meta http-equiv=“expires” content=“0”>      
  18.     <meta http-equiv=“keywords” content=“keyword1,keyword2,keyword3”>  
  19.     <meta http-equiv=“description” content=“This is my page”>  
  20.     <!– 
  21.     <link rel=”stylesheet” type=”text/css” href=”styles.css”> 
  22.     –>  
  23.     <script>  
  24.     </script>  
  25.   </head>  
  26.   <body>  
  27.         <!– 用jsp语句,将servlet传过来的list数据拿到,并放到一个list中 –>  
  28.     <%   
  29.         ArrayList list = (ArrayList) request.getAttribute(“list”);  
  30.     %>  
  31.     <!– 声明一个表格,这是表头 –>  
  32.     <h2 align = “center”>图书列表</h2>  
  33.     <table border = 1px align = “center”>  
  34.         <tr>  
  35.             <th>图书名称</th>  
  36.             <th>图书作者</th>  
  37.             <th>图书价格</th>  
  38.             <th>所属国家</th>  
  39.         </tr>  
  40.         <!– 继续使用jsp语句 循环放入存放于list中的Book实体类中的数据 –>  
  41.         <%  
  42.             for(int i = 0;i<list.size();i++){  
  43.                 Book book =(Book) list.get(i);%>  
  44.                 <tr>  
  45.                 <th><%=book.getName() %></th>  
  46.                 <th><%=book.getAuthor()%></th>  
  47.                 <th><%=book.getPrice()%></th>  
  48.                 <th><%=book.getCountry()%></th><br>  
  49.                   
  50.                 <!– 此处设置了一个修改<a>标签,做修改操作.并将上面拿到的数据传给update.jsp,当进入修改页面的时候,原来的数据会显示 –>  
  51.                 <th><a href=”update.jsp?name=<%=book.getName()%>&author=<%=book.getAuthor()%>&  
  52.                 &country=<%=book.getCountry()%>&price=<%=book.getPrice()%>>修改</a>   
  53.                 <!– 删除操作,只把name字段传给Delete_Servlet.java,用来做删除操作 –>  
  54.                 <a href=“Delete_Servlet?de_name=<%=book.getName()%>” onclick=“confirm(‘确定删除该条记录?’)”>删除</a>  
  55.                 </th>  
  56.         <% }  
  57.          %>  
  58.     </table>  
  59.   </body>  
  60. </html></span></strong><span style=“font-size:24px;”>  
  61. </span>  
<%@page import="com.mi.beans.Book"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'ListBook.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
    <script>
    </script>
  </head>
  <body>
        <!-- 用jsp语句,将servlet传过来的list数据拿到,并放到一个list中 -->
    <% 
        ArrayList list = (ArrayList) request.getAttribute("list");
    %>
    <!-- 声明一个表格,这是表头 -->
    <h2 align = "center">图书列表</h2>
    <table border = 1px align = "center">
        <tr>
            <th>图书名称</th>
            <th>图书作者</th>
            <th>图书价格</th>
            <th>所属国家</th>
        </tr>
        <!-- 继续使用jsp语句 循环放入存放于list中的Book实体类中的数据 -->
        <%
            for(int i = 0;i<list.size();i++){
                Book book =(Book) list.get(i);%>
                <tr>
                <th><%=book.getName() %></th>
                <th><%=book.getAuthor()%></th>
                <th><%=book.getPrice()%></th>
                <th><%=book.getCountry()%></th><br>

                <!-- 此处设置了一个修改<a>标签,做修改操作.并将上面拿到的数据传给update.jsp,当进入修改页面的时候,原来的数据会显示 -->
                <th><a href="update.jsp?name=<%=book.getName()%>&author=<%=book.getAuthor()%>&
                &country=<%=book.getCountry()%>&price=<%=book.getPrice()%>">修改</a> 
                <!-- 删除操作,只把name字段传给Delete_Servlet.java,用来做删除操作 -->
                <a href="Delete_Servlet?de_name=<%=book.getName()%>" οnclick="confirm('确定删除该条记录?')">删除</a>
                </th>
        <% }
         %>
    </table>
  </body>
</html>


            </div>
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
servlet中,可以使用RequestDispatcher的forward方法页面。例如,可以使用以下代码将请求发到名为"EShop.jsp"的JSP页面: ```java String url = "/EShop.jsp"; ServletContext sc = getServletContext(); RequestDispatcher rd = sc.getRequestDispatcher(url); rd.forward(req, res); ``` 这将把请求和响应对象传递给目标页面,使目标页面能够处理请求并生成响应。注意,使用forward方法页面后,地址栏不会发生变化。\[1\] 另外,需要注意的是,forward方式只能到本web应用中的页面上,无法到其他应用或外部网页。如果想要在后传递参数,可以使用url中带parameter、session或request.setAttribute等方法。\[3\] #### 引用[.reference_title] - *1* *2* [Servletjsp页面的几种方法](https://blog.csdn.net/weixin_30713953/article/details/96238984)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Servlet页面](https://blog.csdn.net/weixin_40912987/article/details/116167119)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值