当前位置: 首页 > article >正文

Java Web的三种获取参数的方法

目录

从web.xml文件中获取默认参数

从浏览器的网址界面获取界面参数

继承servlet接口的方法从浏览器的网址界面获取界面参数

继承HttpServlet接口的方法从浏览器的网址界面获取界面参数

从网页表单中获取表单参数

继承Httpservlet中的doPost和doGet各自的作用


从web.xml文件中获取默认参数

首先需要在xml文件中定义和声明,在servlet下的init-parm下的parm-name写入参数名字,parm-value写入参数的值

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>
<!--    创建一个servlet实例,类似java中的new-->
    <servlet>
        <servlet-name>MyServlet01</servlet-name>
        <servlet-class>com.zpark.servlet.MyServlet01</servlet-class>

        <init-param>
            <param-name>name</param-name>
            <param-value>Lungcen</param-value>
        </init-param>
        <init-param>
            <param-name>password</param-name>
            <param-value>110120130</param-value>
        </init-param>
    </servlet>
<!--    配置访问的规则-->
    <servlet-mapping>
        <servlet-name>MyServlet01</servlet-name>
        <url-pattern>/Lun.do</url-pattern>
<!--        .do是针对jsp来的,以.do来访问servlet-->
    </servlet-mapping>
</web-app>

除了web.xml内声明了参数,还要在servlet中利用变量接收参数,再发送到浏览器中显示出来。

package com.zpark.servlet;

import javax.servlet.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

public class MyServlet01 implements Servlet {
    private String name;
    private String password;
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        //获得配置文件中的参数,用一个枚举结构来接收
        Enumeration<String> initParameterNames = servletConfig.getInitParameterNames();
        while(initParameterNames.hasMoreElements())
        {
            String tump = initParameterNames.nextElement();
            if ("name".equals(tump))
                name = servletConfig.getInitParameter(tump);
            if ("password".equals(tump))
                password = servletConfig.getInitParameter(tump);
        }
    }
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse)
            throws ServletException, IOException {
        //每次请求,都会调用一次 service() 方法,设置字符集
        servletResponse.setContentType("text/html;charset=UTF-8");
        //使用PrintWriter.write()方法向前台页面输出内容
        PrintWriter writer = servletResponse.getWriter();
        writer.write("学习servlet,开启新征程 <br />");
        writer.write("name ->" + name + "<br />");
        writer.write("password ->" + password);
        writer.close();
    }
    @Override
    public ServletConfig getServletConfig() {
        return null;
    }
    @Override
    public String getServletInfo() {
        return null;
    }
    @Override
    public void destroy() {
    }
}

从浏览器的网址界面获取界面参数

继承servlet接口的方法从浏览器的网址界面获取界面参数

package com.zpark.servlet;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

public class MyServlet01 implements Servlet {
//    继承servlet的方法不是很常用

    private String IU_name;
    private String IU_password;

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        //获得配置文件中的参数,用一个枚举结构来接收
        Enumeration<String> initParameterNames = servletConfig.getInitParameterNames();
        while(initParameterNames.hasMoreElements())
        {
            String tump = initParameterNames.nextElement();
            if ("name".equals(tump))
                IU_name = servletConfig.getInitParameter(tump);
            if ("password".equals(tump))
                IU_password = servletConfig.getInitParameter(tump);
        }
    }

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse)
            throws ServletException, IOException {

        //向下转型
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;

        //设置请求编码
        httpServletRequest.setCharacterEncoding("utf-8");

        String name;
        String password;
        String name_hsr = httpServletRequest.getParameter("name");

        if(name_hsr != null && !"".equals(name_hsr) && !"null".equals(name_hsr) )
            name = httpServletRequest.getParameter("name");
        else
            name = IU_name;

        String password_hsr = httpServletRequest.getParameter("password");

        if(password_hsr != null && !"".equals(password_hsr) && !"null".equals(password_hsr) )
            password = httpServletRequest.getParameter("password");
        else
            password = IU_password;

        //设置下响应字符集编码
        servletResponse.setContentType("text/html;charset=UTF-8");
        //使用PrintWriter.write()方法向前台页面输出内容
        PrintWriter writer = servletResponse.getWriter();
        writer.write("学习servlet,开启新征程 <br />");
        writer.write("name ->" + name + "<br />");
        writer.write("password ->" + password);
        writer.close();
    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {
    }
}

继承HttpServlet接口的方法从浏览器的网址界面获取界面参数

package com.zpark.servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

//这个类似于再web.xml文件的配置的访问路径和参数的对象和值
@WebServlet(value = {"/ms4.do", "/ms5.do"},
        initParams = {@WebInitParam(name = "name", value = "默认姓名(赵三)"),
                @WebInitParam(name = "password", value = "默认密码(110120130)")}
)
public class MyServlet4 extends HttpServlet {
    private String IU_name = "";
    private String IU_password = "";
    @Override
    public void init(ServletConfig config) throws ServletException {
        Enumeration<String> ParameterNames = config.getInitParameterNames();
        while (ParameterNames.hasMoreElements())
        {
            //如果还存在元素,就拿出元素
            String temp = ParameterNames.nextElement();
            if ("name".equals(temp))
                IU_name = config.getInitParameter(temp);
            if ("password".equals(temp))
                IU_password = config.getInitParameter(temp);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String name;
        String password;
        //设置响应对象的编码字符集
        resp.setContentType("text/html;charset=utf-8");
        //设置请求对象的编码字符集
        resp.setCharacterEncoding("UTF-8");
        //输出信息
        PrintWriter writer = resp.getWriter();
        String um = req.getParameter("name");
        String hsr = req.getParameter("password");
        if ( um != null && !"".equals(um) && !"null".equals(um) )
            name = um;
        else
            name = IU_name;
        if ( hsr != null && !"".equals(hsr) && !"null".equals(hsr) )
            password = hsr;
        else
            password = IU_password;
        writer.write("username:" + name);
        writer.write("<br/>");
        writer.write("password:" + password);
        writer.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        //设置请求编码
        req.setCharacterEncoding("utf-8");
        this.doGet(req, resp);
    }
}

从网页表单中获取表单参数

继承Httpservlet中的doPost和doGet各自的作用

doPost是接收请求,请求是由<form>表单标签来发送请求。

doGet是接收响应,响应给浏览器,所以再浏览器网址回车的时候,会调用diGet然后响应给浏览器。

<%--    method可以选择是post还是get,但是我们一般是post请求--%>
<form action="/uned_war/ms3.do" method="post">
        用户:<input type="text" name="username" />
        密码:<input type="password" name="password" />
        <input type="submit" value="提交">
</form>

在jsp文件中写入表单,然后再浏览器的表单输入参数

在servlet中接收参数然后再响应到浏览器

<%@page language="java" pageEncoding="UTF-8" %>
<html>
<head>
    <title>ti mu</title>
</head>
<body>
    <form action="/unwar/ms3.do" method="post">
        用户:<input type="text" name="username" />
        密码:<input type="password" name="password" />
        <input type="submit" value="提交">
    </form>
</body>
</html>
package com.zpark.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class MyServlet3 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        //获取界面的内容
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //设置响应对象的编码字符集
        resp.setContentType("text/html;charset=utf-8");
        //输出信息
        PrintWriter writer = resp.getWriter();
        writer.write("Hello world" + "<br/>");
        writer.write("username ->" + username + "<br/>");
        writer.write("password ->" + password + "<br/>");
        writer.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        //设置请求编码
        req.setCharacterEncoding("UTF-8");
        doGet(req, resp);
    }
}


http://www.kler.cn/a/4469.html

相关文章:

  • Python 实现 NLP 的完整流程
  • 通过将模型权重的矩阵表示为低秩矩阵,可以减少需要调整的参数数量,通俗易懂的解释,不懂你爬网线打我
  • Elasticsearch技术标准解析与实践案例
  • Sentaurus TCAD学习笔记:transform指令
  • Vue2+OpenLayers使用Overlay实现点击获取当前经纬度信息(提供Gitee源码)
  • 如何将原来使用cmakelist编译的qt工程转换为可使用Visual Studio编译的项目
  • Linux部署Docker
  • 分组函数·union·limit·order by排序·group by分组·外键
  • 面试字节跳动软件测试岗,收到offer后我却毫不犹豫拒绝了....
  • day15-面向对象作业2
  • 常用的 IntelliJ IDEA 快捷键
  • Element Plus 实例详解(三)___Date Picker 日期选择
  • 大数据应用——Hadoop运行模式(本地运行)
  • nacos入门
  • SpringBatch-Demo1
  • 多线程的几种状态
  • 2023最新版360度无死角python学习路线
  • Python提取文本文件中某个位置的数据
  • 简化代码,提高效率:C++ auto关键字的魅力
  • 短信宝接入发送短信实测 Java
  • 【操作系统】一文带你深入浅出零拷贝技术
  • Apache POI 入门·第一话
  • Autosar诊断-简介和功能概述
  • 网络基础知识
  • 应用层 —— HTTP协议
  • Spark大数据处理讲课笔记2.3 Spark运行架构与原理