(Java Web) 提交表单 实例讲解

article/2025/9/21 14:04:43

传输过程 表单 to Servlet

开门见山,一张图解释

在这里插入图片描述

首先设置表单的<form action="myURL/toText" method="post">

此处的action对应web.xml中的url-pattern web.xml中需要注册好对应的Servlet

method可设置post 或者 get不设置的话默认是get

后文添加的其他文件的web.xml注册代码不在展示

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>表单提交</title>
</head>
<body><form action="myURL/toText" method="post">姓名<input type="text" name="name"> <br>爱好 <input type="checkbox" name="hobby" value="read"> 阅读<input type="checkbox" name="hobby" value="run"> 跑步<input type="checkbox" name="hobby" value="music"> 音乐<br>该课程名称<input type="radio" name="course" value="Java Web"/>Java Web<input type="radio" name="course" value="Android"/>Android<input type="radio" name="course" value="PHP"/>PHP<br>交卷请单击:<input type="submit" value="交卷"/>重答请单击:<input type="reset" value="重答"/></form>
</body>
</html>
<?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" 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>000</display-name><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list><servlet><servlet-name>Register</servlet-name><servlet-class>test.com.Register</servlet-class></servlet><servlet-mapping><servlet-name>Register</servlet-name><url-pattern>/myURL/*</url-pattern></servlet-mapping>
</web-app>

访问index:http://localhost:8080/000/index.html

在这里插入图片描述

Servlet中接受处理

package test.com;import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class Register extends HttpServlet {private static final long serialVersionUID = 1L;public Register() {super();}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//解决中文乱码,设置字符编码request.setCharacterEncoding("utf-8");//request存储的是一个键值对,key:String val:String[]//getParameter 根据键获得值String name = request.getParameter("name");System.out.println("name=" + name);//空值为nullString age = request.getParameter("age");System.out.println("age=" + age);System.out.println("********************");//枚举类型,获得请求参数的列表Enumeration<String> pNames = request.getParameterNames();while(pNames.hasMoreElements()){String pname = pNames.nextElement();//如果参数名称的值有多个,则获取第一个String namevalue = request.getParameter(pname);System.out.println(pname + "=====" + namevalue);}System.out.println("********************");//一般用于获取复选框String[] pValues = request.getParameterValues("hobby");for (String hobby : pValues){System.out.println(hobby);}System.out.println("********************");//获得所有键值对Map<String, String[]> pMap = request.getParameterMap();//获得所有键Set<String> keySet = pMap.keySet();for (String mpname : keySet){String[] values = pMap.get(mpname);System.out.print(mpname + "===");for (String value : values){System.out.print(value + " ");}System.out.println();}}}

控制台输出

name=张三
age=null


name=张三
hobby
=read
course=====Java Web


read
run
music


name=张三
hobby
=read run music
course===Java Web

request的常用方法

setCharacterEncoding

设置字符集,放置中文导致的乱码

request.setCharacterEncoding(“utf-8”);

getParameter

根据参数key获得val,如果没有该键值对接受内容为null

String name = request.getParameter(“name”);

getParameterNames

枚举类型,获得请求参数的列表

Enumeration pNames = request.getParameterNames();

getParameterValues

一般用于获取复选框 用String[]形式接受

String[] pValues = request.getParameterValues(“hobby”);

getParameterMap

获得所有键值对

Map<String, String[]> pMap = request.getParameterMap();

Servlet间互传 (域属性)

第一个Servlet

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//获取对象String name = request.getParameter("name");String age = request.getParameter("age");//理解上可以类比成一个Map//放入requset的域属性中//作用域就是一次请求的时间request.setAttribute("name", name);request.setAttribute("age", age);//删除属性request.removeAttribute("age");//当前请求转发到另一个Servlet中(还是在Servlet中没有返回浏览器,算这一次请求)request.getRequestDispatcher("/otherServlet").forward(request, response);}

第二个Servlet

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//设置字符集response.setContentType("text/html;charset=utf-8");request.setCharacterEncoding("utf-8");//获取从另一个Servlet中传入的数据String name = (String)request.getAttribute("name");String age = (String)request.getAttribute("age");System.out.println("name = " + name);System.out.println("age = " + age);//字符集要在生成PrintWriter对象之前使用PrintWriter out = response.getWriter();out.println("可放html代码");		//也要处理乱码out.append("show code");out.println();		//只是空格out.print("<br/>");	//真换行	out.print("name = " + name);out.print("<br/>");out.write("age = " + age);//		再次跳转 可以跳转到html的页面
//		request.getRequestDispatcher("/welcome.html").forward(request, response);}

在这里插入图片描述

request路径相关的方法

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {StringBuffer requestURL = request.getRequestURL();System.out.println("requestURL == " + requestURL);//资源路径String requestURI = request.getRequestURI();System.out.println("requestURI == " + requestURI);//获得请求的上下文路径(就是项目名)String contextPath = request.getContextPath();System.out.println("contextPath == " + contextPath);//获取客户端的ip地址String remoteAddr = request.getRemoteAddr();System.out.println("remoteAddr == " + remoteAddr);//Servlet的路径	精确部分,就是在web.xml中设置的urlString servletPath = request.getServletPath();System.out.println("servletPath == " + servletPath);//URI中去掉精确部分String pathInfo = request.getPathInfo();System.out.println("pathInfo == " + pathInfo);//请求的方式 GET或者POSTString method = request.getMethod();System.out.println("method == " + method);}

输出

这里以本文开头图片中的表单的url提交,注意文本代码所在的项目名是000

​ 项目名 url的精确部分 非精确部分

URI = ServletContext + ServletPath + PathInfo

requestURL == http://localhost:8080/000/myURL/toText
requestURI == /000/myURL/toText
contextPath == /000
remoteAddr == 0:0:0:0:0:0:0:1
servletPath == /myURL
pathInfo == /toText
method == POST


http://chatgpt.dhexx.cn/article/mqm5O7Zx.shtml

相关文章

微信小程序提交表单

先看效果 身份选择使用了picker 具体代码&#xff1a; form.wxml <view class"modify-form"> <form bindsubmitgetForm><view class"label-list"><view class"label">姓名</view><view class"input-b…

Html提交表单的制作

Html提交表单制作 笔记整理 1. 代码 <!-- 知识总结&#xff1a;提交表单中form必须有action属性&#xff0c;表示提交地址所有提交的数据&#xff0c;input必须具有name属性&#xff0c;才能把数据提交到指定地址input按钮的文字&#xff0c;使用value表示属性input必须放…

异步提交表单

异步提交表单 异步提交表单的步骤 所谓异步提交表单&#xff0c;就是不再使用表单的提交按钮实现表单的提交功能&#xff0c;而是通过Ajax异步交互方式实现表单提交。具体实现步骤如下: 获取表单及所有表单组件对应的数据值。将所有表单组件对应的数据值拼成特定格式的字符串…

按钮提交表单

2.3 提交表单数据 ASP.NET Framework包含三个用于向服务器端提交表单的控件&#xff1a;Button、LinkButton和ImageButton。这三个控件拥有同样的功能&#xff0c;但每种控件的外观截然不同。 本节学习如何在页面中使用这三种控件。然后&#xff0c;学习如何关联客户端脚本和服…

from 表单提交

因为是转载文章 在此标明出处&#xff0c;以前有文章是转的没标明的请谅解&#xff0c;因为有些已经无法找到出处&#xff0c;或者与其它原因。 如有冒犯请联系本人&#xff0c;或删除&#xff0c;或标明出处。 因为好的文章&#xff0c;以前只想收藏&#xff0c;但连接有时候会…

HTML_表单与提交

<!-- form 标签 action 提交地址 method 提交方式get 高效 但数据会在url中显示 且传输量小post 效率较低 但数据不在url中显示 且传输量大 --> <!--name属性作为提交数据时数据的变量名--> <!--value属性作为填充值(多种意义上)--> <!--单选框/多选框中…

form表单的提交

开发工具与关键技术&#xff1a;MVC JQuery 的 form表单的提交 一、Form表单有两个属性分别是&#xff1a;“action”和“method”: Action: 的值是URL 就是当提交表单时向某个地方&#xff08;要提交到某处的地址&#xff09;发送表单数据 Method: 的值是 get和 post 就是用来…

表单提交的方法

form表单有两种属性action与method。 action属性有一个值URL。它规定当提交表单时向何处放送表单数据&#xff0c;URL有两种值&#xff1a;一种绝对URL&#xff0c;一种相对URL。 绝对URL指向其他站点(比如 srcwww.baidu.com网址)。 相对URL指向站点内的文件(比如 src&#…

form表单提交的几种方式

表单提交方式一&#xff1a;直接利用form表单提交 html页面代码&#xff1a; <!DOCTYPE html> <html> <head> <meta charset"UTF-8" /> <title>Insert title here</title> </head> <body> <form action"ht…

extremecomponents -- 文档下载依赖使用

extremecomponents – 文档下载依赖使用 jar包下载链接: https://mvnrepository.com/artifact/org.extremecomponents/extremecomponents. https://blog.csdn.net/yu__yfchun125/article/details/7655593

extremecomponents相关大全

安装要求1、Servlet 2.3 或更高2、 JDK 1.3.1 或更高 最小的Jars需求1、commons-beanutils 1.62、commons-collections 3.03、 commons-lang 2.04、 commons-logging 1.0.45、 standard 1.0.2 PDF 导出要用到的包:1、 avalon-framework 4.02、batik 1.5-fop-0.20-53、 fop 0.2…

ExtremeComponents源码解析(一)

一、前言 因参与公司框架改造&#xff0c;在负责前端table组件选型时&#xff0c;原本选了jqGrid和Bootstraptable作为备选方案&#xff0c;评审会上&#xff0c;武哥提了EXtremeComponents&#xff0c;让我也去了解下&#xff0c;看下合不合适&#xff0c;在此机缘下&#xff…

eXtremeComponents的eXtremeTable分页特性

<script type"text/javascript"> </script> <script type"text/javascript" src"http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> eXtremeComponents的eXtremeTable是一套很好的分页标签&#xf…

Android Dialog

创建对话框 Showing a Dialog 显示对话框 Dismissing a Dialog 解除对话框 Using dismiss listeners 使用解除监听器Creating an AlertDialog 创建警告对话框 Adding buttons 增加按钮 Adding a list 增加列表 Adding checkboxes and radio buttons 增加单选框和复选框Creating…

关于DialogResult

在程序中&#xff0c;经常会弹出一个对话框来让用户填写一些信息&#xff0c;填写完成后&#xff0c;当用户点击“确定”按钮后&#xff0c;在主窗体中进行其他的处理。比如一个简单的例子&#xff0c;在主窗体中有一个菜单&#xff0c;是“增加用户”&#xff0c;当点击这个菜…

dialogFragment---dialog

详解一&#xff1a; Android提供alert、prompt、pick-list&#xff0c;单选、多选&#xff0c;progress、time-picker和date-picker对话框&#xff0c;并提供自定义的dialog。在Android 3.0后&#xff0c;dialog基于fragment&#xff0c;并对之前版本提供兼容支持库&#xff0c…

Android Dialog

Android Dialog 创建对话框 一个对话框一般是一个出现在当前Activity之上的一个小窗口. 处于下面的Activity失去焦点, 对话框接受所有的用户交互. 对话框一般用于提示信息和与当前应用程序直接相关的小功能. Android API 支持下列类型的对话框对象: 警告对话框 AlertDialog: 一…

Dialogs(对话框)

对话框 对话框是一种提示用户去做出选择或输入其他信息的小窗口。 对话框不填充屏幕并且通常被用于在执行前需要用户做出决定的模态事件。 对话框设计 阅读 Dialogs 设计指南&#xff0c;获取包括语言规范等关于如何设计对话框的更多信息。 虽然 Dialog 类是对话框的基类&#…

【Android Dialog】Dialog

AlertDialog Dialog类是所有弹窗的父类&#xff0c;官方建议我们不要直接实例化它&#xff0c;而是使用其子类来获取实例。AlertDialog是系统提供的一个直接子类&#xff0c;它能帮助我们快速构建出不同类型的弹窗。接下来就看下各种类型弹窗的使用。 1、普通对话框 /*** Al…

DIALOG

[SAP]屏幕Dynpro 原文链接&#xff1a;http://www.cnblogs.com/jiangzhengjun/p/4292250.html 对话屏幕Dynpro(SE51). 11 屏幕元素... 11 屏幕属性... 11 PAI事件的触发、屏幕元素Function Code设置... 12 屏幕流逻辑Screen Flow Logic. 12 对话屏幕中的字段命名大小写问题...…