参数获取

Posted houtian2333

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了参数获取相关的知识,希望对你有一定的参考价值。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <!--全局初始化参数-->
    <context-param>
        <param-name>a</param-name>
        <param-value>全局初始化参数</param-value>
    </context-param>


    <filter>
        <filter-name>login</filter-name>
        <filter-class >filter.LoginFilter</filter-class>
        <!--过滤器中参数-->
        <init-param>
            <param-name>uri</param-name>
            <param-value>update,updateDo,delete</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>login</filter-name>
        <url-pattern>/user</url-pattern>
    </filter-mapping>
    <!--过滤器执行-->


    <servlet>
        <servlet-name>MyServlet</servlet-name>
        <servlet-class>MyServlet</servlet-class>
        <init-param>
            <!--servlet初始化参数-->
            <param-name>charset</param-name>
            <param-value>servlet初始化参数</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
      <!-- 映射 -->
        <servlet-name>MyServlet</servlet-name>
        <url-pattern>/my</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>UserController</servlet-name>
        <servlet-class>UserController</servlet-class>
        <!--在服务器启动的时候就初始化servlet对象 值为初始化顺序-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>UserController</servlet-name>
        <url-pattern>/user/*</url-pattern>
    </servlet-mapping>
    <!--执行登录界面-->
</web-app>


//在servlet获取参数

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class MyServlet extends javax.servlet.http.HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        doGet( request,response);
    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        //获取servlet初始化参数
        ServletConfig servletConfig = getServletConfig();
        String v = servletConfig.getInitParameter("charset");
        System.out.println(v);

        //获取全局初始化参数
        ServletContext servletContext = getServletContext();
        String  value = servletContext.getInitParameter("a");
        System.out.println(value);

    }
}


//在过滤器中获取参数
  
package filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/*
*
* 登录过滤器
* */
public class LoginFilter implements Filter {
    private List<String> list;
    /*
    * 过滤器实例化以后会执行一次
    * */
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        list =new ArrayList<>();
       /* list.add("update");
        list.add("updateDo");
        list.add("delete");*/

    //获取过滤器初始化参数
       String value = filterConfig.getInitParameter("uri");//uri 就是url除去协议端口的剩下部分
       String[] uris =value.split(",");   //数组
       for(String s:uris){
           list.add(s);
       }


    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)servletRequest;  //强转类型
        HttpServletResponse response =( HttpServletResponse)servletResponse;

        //获取全局初始化参数
        String a = request.getServletContext().getInitParameter("a");
        System.out.println(a);

        String m = servletRequest.getParameter("m");
        if(list.contains(m)){  //判断如果存在这个参数
            HttpSession session =request.getSession();
            Object o = session.getAttribute("C_USER");
            if(o ==null){
                response.sendRedirect("/web/user?m=login");
                return;//退出该方法
            }

        }
        filterChain.doFilter(request,response);  //执行该该请求接下来的事情
    }

    @Override
    public void destroy() {

    }
}

 

以上是关于参数获取的主要内容,如果未能解决你的问题,请参考以下文章

js获取url的参数

常用python日期日志获取内容循环的代码片段

JS获取URL的参数

片段(Java) | 机试题+算法思路+考点+代码解析 2023

xml Eclipse模板(代码片段)检查参数并最终抛出IllegalArgumentException

Visual Studio 自定义代码片段在方法定义的参数列表中不起作用