获取表单对象的值
来源:互联网 发布:淘宝直播小二钉钉群 编辑:程序博客网 时间:2024/05/18 02:34
工程名:ServletStudy
表单文件:login.jsp
Servlet文件:LoginServlet.java
配置文件:web.xml
下面对是这几个文件的代码:
login.jsp
表单文件:login.jsp
Servlet文件:LoginServlet.java
配置文件:web.xml
下面对是这几个文件的代码:
login.jsp
<%@ 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 'login.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"> --> </head> <body> <form action="/ServletStudy/LoginServlet"> userName: <input name="userName" type="text"/><br/> password: <input name="password" type="password"/><br/> <input type="submit" value="submit"/> <input type="reset" value="reset"/> </form> </body></html>LoginServlet.java
package com;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;@SuppressWarnings("serial")public class LoginServlet extends HttpServlet{ protected void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException { String username = req.getParameter("userName"); String password = req.getParameter("password"); resp.setContentType("text/html"); PrintWriter out = resp.getWriter(); out.println("<html><head><title>Login Result</title></head"); out.println("<body> username:"+username+"<br>"); out.println("password:"+password+"<br></body></html>"); out.flush(); }}
web.xml
在二级标签中添加以下代码:
<servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>LoginServlet</servlet-name> <servlet-class>com.LoginServlet</servlet-class> </servlet><servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/LoginServlet</url-pattern> </servlet-mapping>
说明:
1.<form>标签中的action值为web.xml中的<url-pattern></url-pattern>中的值。再加上Servlet类名,如这里的是
/ServletStudy/LoginServlet
提交的方式默认为get方式,本例中即是以get方式提交的,这里没有对中文问题进行处理。但建议一般使用post方式提交表单。
2. String username =req.getParameter("userName");
这里的参数值“userName”要与<inputname="userName"type="text"/>中的name属性值相同
下面是使用doPost提交方式,关键部分代码为:
protected void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String username = req.getParameter("userName"); String password = req.getParameter("password"); resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); //设置返回给客户端的文本格式,可解决文字编码格式不统一的问题 PrintWriter out = resp.getWriter(); out.println("<html><head><title>Login Result</title></head"); out.println("<body> username:"+username+"<br>"); out.println("password:"+password+"<br></body></html>"); out.flush(); }若使用doPost方式响应客户,则要将<form>表单中的method的值改为post。
0 0
- 获取表单对象的值
- 常用的jquery获取表单对象的属性与值
- jQuery获取表单对象的属性与值
- js获取表单对象的方法
- 使用request对象获取表单的信息
- js表单序列化,jquery获取表单对象的属性值
- js获取表单对象
- js获取表单的值
- 表单元素值的获取
- JQ获取表单的值
- 【jquery】常用的jquery获取表单对象的属性与值
- Action中获取HttpServletRequest对象的方法(表单提交值,url传递参数)
- 获取表单的值或内容
- 数组型表单控件的值获取
- jQuery获取form表单元素的值
- jQuery获取form表单元素的值
- jquery获取input表单值的代码
- jquery获取表单值的利器:serialize()
- sizeof 与 strlen 的区别
- 第一章 【面向对象软件工程与UML】
- 数据仓库系列 - 缓慢渐变维度 (Slowly Changing Dimension) 常见的三种类型及原型设计
- 【Linux入门学习之】vi/vim编辑器必知必会
- 利用Matplotlib和ImageMagick制作gif动画
- 获取表单对象的值
- 让struts2和servlet共存
- MAJOR、MINOR、MKDEV理解
- Webdnpro UI控件开发篇 Table控件使用四(fixedBottomCell)
- Kinect骨骼关节数据的获取
- 上班后的第一个星期日
- transform属性 -- css属性详解系列(3)
- NYOJ44 - 子串和(最大连续子串和)
- ecg-id实验系统