• 界面间的参数传递


    servlet和jsp页面间的参数传递

    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@page import="java.sql.*" %>  <%--导入java.sql包--%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <title>需求浏览</title>
    <script type="text/javascript" src="layui/layui.js"></script>
    <link rel="stylesheet" href="layui/css/layui.css">
    </head>
    <body style="margin-top:15px;margin-left: 10px;">
    <div style="text-align: center;">
    <h1>需求浏览</h1>
    <table class="layui-table" >
      <thead>
        <tr>
          <th>需求名称</th>
          <th>机构名称</th>
          <th>所在地域</th>
          <th>归口管理单位</th>
          <th>机构属性</th>
          <th>科技活动类型</th>
          <th>形式审核状态</th>
          <th>部门审核状态</th>
          <th>操作</th>
        </tr> 
      </thead>
      <tbody>
        <%  
            try {  
                Class.forName("com.mysql.jdbc.Driver");  //驱动程序名
                 String db_url = "jdbc:mysql://localhost:3306/sys?&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
                
                 String db_user = "root";
                
                 String db_pass = "123456";
                Connection conn = DriverManager.getConnection(db_url, db_user, db_pass);
    
                if(conn != null){  
                    //out.print("数据库连接成功!");  
                    //out.print("<br />");
                    Statement stmt = null;  
                    ResultSet rs = null;  
                    String sql = "select * from  Biao where  oname=?;";  //查询语句
                      
                    PreparedStatement prpe=(PreparedStatement) conn.prepareStatement(sql);
                    
                    prpe.setString(1,request.getParameter("workplace"));
                    System.out.println(request.getParameter("workplace"));
                    System.out.println(request.getParameter("tell"));
                    rs = prpe.executeQuery();  
                    String stus=null;//部门审核
                    String stus2=null;//形式审核
                    //out.print("查询结果:");  
                    //out.print("<br/>");
                    while (rs.next()){
                        if(rs.getString("shen").equals("同意")||rs.getString("shen").equals("不同意")){
                            stus="已审核";
                        }else{
                            stus="未审核";
                        }
                        if(rs.getString("shen2").equals("同意")||rs.getString("shen2").equals("不同意")){
                            stus2="已审核";
                        }else{
                            stus2="未审核";
                        }
                        if(rs.getString("shen").equals("未审核")||stus.equals("已审核")){
    %>
                        <tr data-id="1">
                            <td><%=rs.getString("demandname") %></td>
                            <td><%=rs.getString("oname") %></td>
                            <td><%=rs.getString("saddress") %></td>
                            <td><%=rs.getString("sub") %></td>
                            <td><%=rs.getString("attribute") %></td>
                            <td><%=rs.getString("type") %></td>
                            <td class="td-status" >
                                <span class="layui-btn layui-btn-normal layui-btn-xs"><%=stus2 %></span>
                            </td>
                            <td class="td-status" >
                                <span class="layui-btn layui-btn-normal layui-btn-xs"><%=stus %></span>
                            </td>
                            <td class="td-manage">
                                
                                <a title="查看"  href="${pageContext.request.contextPath}/BrowserServlet?method=search&way=look&id=<%=rs.getInt("id") %>&oname=<%=request.getParameter("workplace") %>">
                                    <i class="layui-icon layui-icon-edit"></i>
                                </a>
                                <a  title="修改" href="${pageContext.request.contextPath}/BrowserServlet?method=search&way=modify&id=<%=rs.getInt("id") %>&oname=<%=request.getParameter("workplace") %>&bu=<%=stus %>&xing=<%=stus2 %>">
                                    <i class="layui-icon layui-icon-util"></i>
                                </a>
                                <a title="删除" onclick="member_del(this,'要删除的id')" href="javascript:;">
                                    <i class="layui-icon layui-icon-delete"></i>
                                </a>
                                
                            </td>
                        </tr>
                        
                        <%
                        }
                }  
                }else{  
                    out.print("连接失败!");  
                }  
            }catch (Exception e) {        
                //e.printStackTrace();  
                out.print("数据库连接异常!");  
            }  
    %> 
      </tbody>
    </table>
    
    </body>
    
    <script type="text/javascript">
    
    //修改提示弹窗
    
    var te='<%=request.getParameter("tell") %>';
    if(te=="no")
    {
        alert("正在审核中不可修改!");
    }
    
    </script>
    </html>

    severlt代码

    package com.servlet;
    
    import java.io.IOException;
    import java.sql.SQLException;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import com.bean.AdminBean;
    import com.bean.AdminnewBean;
    import com.bean.BiaoBean;
    
    
    import com.dao.BiaoDao;
    
    import com.mysql.fabric.xmlrpc.base.Array;
    /**
     * Servlet implementation class AdminServlet
     */
    @WebServlet("/BrowserServlet")
    public class BrowserServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public BrowserServlet() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            response.getWriter().append("Served at: ").append(request.getContextPath());
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            req.setCharacterEncoding("utf-8");
            String method=req.getParameter("method");
            String oname=req.getParameter("oname");
            System.out.print(method);
            
            if(method.equals("all"))
            {
                try {
                    all(req,resp);
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    String msg = "登录错误";
    
                    req.setAttribute("msg",msg);
    
                    req.getRequestDispatcher("/error.jsp").forward(req,resp);
                }
            }
            else if(method.equals("search")) {
                try {
                    search(req,resp);
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    String msg = "登录错误";
    
                    req.setAttribute("msg",msg);
    
                    req.getRequestDispatcher("/error.jsp").forward(req,resp);
                }
            }
            
        }
        protected void all(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
            req.setCharacterEncoding("utf-8");
            String username=req.getParameter("username");
            String password=req.getParameter("password");
            //System.out.println(username);
            //System.out.println(password);
            List<BiaoBean> biao=new ArrayList<BiaoBean>();
            BiaoDao biaodao=new BiaoDao();
            biao=biaodao.all();
            req.setAttribute("biao",biao);
            req.getRequestDispatcher("/browser.jsp").forward(req,resp);
        }
        protected void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
            //审核界面(edit.jsp)、修改界面(editmodify.jsp)、查看界面(editlook.js)需要预显示的数据
            req.setCharacterEncoding("utf-8");
            resp.setCharacterEncoding("utf-8");
            resp.setContentType("text/html");
            String id=req.getParameter("id");//通过id唯一标识需求表
            String way=req.getParameter("way");
            
            String username=req.getParameter("username");
            String oname=req.getParameter("oname");
            String bu=req.getParameter("bu");
            String xing=req.getParameter("xing");
            System.out.println(username);
            System.out.println(bu);
            System.out.println(xing);
            System.out.println(way);
            
            System.out.println(oname);
            BiaoBean biao=new BiaoBean();
            BiaoDao biaodao=new BiaoDao();
            biao=biaodao.search(id);
            req.setAttribute("biao", biao);
            if(way.equals("shen")) {
                req.setAttribute("use","bu");
                req.setAttribute("username",username);
                req.getRequestDispatcher("/edit.jsp?").forward(req,resp);//部门审核界面
            }
            else if(way.equals("shen2"))
            {
                req.setAttribute("use","xing");
                req.getRequestDispatcher("/edit.jsp").forward(req,resp);//形式审核界面
            }
            else if(way.equals("look"))
            {
                req.setAttribute("oname",oname);
                req.getRequestDispatcher("/editlook.jsp").forward(req,resp);//注册用户查看需求表
            }
            else if(way.equals("modify"))
            {
                if(xing.equals("已审核")) {//如果形式审核员已审核则不可修改
                
                    req.getRequestDispatcher("/tablesettle.jsp?tell=no&workplace="+oname).forward(req,resp);
                }
                else {
                    req.setAttribute("oname",oname);
                    req.getRequestDispatcher("/editmodify.jsp").forward(req,resp);//注册用户修改需求表
                }
            }
        }
    }

    jsp中可通过herf跳转时并携带参数到servlet

    href="${pageContext.request.contextPath}/BrowserServlet?method=search&way=modify&id=<%=rs.getInt("id") %>&oname=<%=request.getParameter("workplace") %>&bu=<%=stus %>&xing=<%=stus2 %>"

    1、直接通过method=search  等号前面是变量名,等号后面是变量的值

    2、是id=<%=rs.getInt("id") %>和bu=<%=stus %> 都是等号前面是变量名,等号后面是变量的值,且这里通过<%=  %> 里面就可以写java的代码

    3、oname=<%=request.getParameter("workplace") %> 这里也是通过<%=  %> 里面就可以写java的代码 ,request.getParameter("workplace")就是可以用来获取所有别的地方传到该界面的参数

    servlet中可通过

    req.setAttribute("oname",oname);

    和req.getRequestDispatcher("/tablesettle.jsp?tell=no&workplace="+oname).forward(req,resp);即在地址后面携带参数传递

    这里我发现一个问题

    servlet中可通过req.setAttribute("tell","no");传递到jsp时

    这里输出为空即没有接受到值

    而下面js中

    var guan='${tell}';alert(guan);能接受到值并能弹窗显示

    而当servlet中通过req.getRequestDispatcher("/tablesettle.jsp?tell=no&workplace="+oname).forward(req,resp);传递到jsp时

    这里能够获取到值

    而下面jsp中var guan='${tell}';alert(guan);却接受不到值

    换为var guan='<%=request.getParameter("tell") %>'就可以接受到值了

  • 相关阅读:
    ffmpeg 简单使用总结
    使用 Solr 构建企业级搜索服务器
    Linux 常用命令整理
    基于.net standard 的动态编译实现
    基于.net core 微服务的另类实现
    Java中线程总结
    关于EF中直接执行sql语句的参数化问题
    一个关于单据审核的流程演变
    java中匿名内部类总结
    Eclipse 中打开选中文件/文件夹所在目录
  • 原文地址:https://www.cnblogs.com/chenaiiu/p/13881441.html
Copyright © 2020-2023  润新知