1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之13.Form表单处理(1)

Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之13.Form表单处理(1)

时间:2023-07-28 15:41:18

相关推荐

Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之13.Form表单处理(1)

–Form 表单简介

–创建并提交表单

–使用Servlet处理表单

• 读取单个请求参数

• 读取多个表单

• 读取所有参数名称

–实例

• 注册会员 ###############Michael分割线#################–Form 表单简介

• 表单是web程序和用户交互的主要途径之一,例如:搜索引擎、用户登录、注册等操作都离不开表单的使用

• 常用表单元素

–input

» text

» password

» radio

» checkbox

» hidden

» file

» button

» reset

» submit

–select

» option

–textarea• 创建并提交表单

–一个简单的表单(例如:登录),如下所示

–当单击“登录”按钮时表单被提交• 使用Servlet处理表单

–读取单个请求参数

• String user = request.getParameter(“user”);RegisterServlet.javapackage com.michael.servletform;

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;

public class RegisterServlet extends HttpServlet {

/**

* Constructor of the object.

*/

public RegisterServlet() {

super();

}

/**

* Destruction of the servlet. <br>

*/

public void destroy() {

super.destroy(); // Just puts "destroy" string in log

// Put your code here

}

/**

* The doGet method of the servlet. <br>

*

* This method is called when a form has its tag value method equals to get.

*

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

String username = request.getParameter("username");

String password = request.getParameter("password");

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out

.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");

out.println("<HTML>");

out.println("<HEAD><TITLE>A Servlet</TITLE></HEAD>");

out.println("<BODY>");

out.println("username:"+username);

out.println("username:"+password);

out.println("</BODY>");

out.println("</HTML>");

out.flush();

out.close();

}

/**

* The doPost method of the servlet. <br>

*

* This method is called when a form has its tag value method equals to post.

*

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doGet(request,response);

}

/**

* Initialization of the servlet. <br>

*

* @throws ServletException if an error occure

*/

public void init() throws ServletException {

// Put your code here

}

}register.html<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>register.html</title>

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="this is my page">

<meta http-equiv="content-type" content="text/html; charset=UTF-8">

<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

</head>

<body>

<form action="/Servlet_Form/servlet/RegisterServlet" method="post">

用户名称:<input type="text" name="username"><br>

用户密码:<input type="password" name="password"><br>

<input type="submit" value="注册">

</body>

</html>web.xml<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4"

xmlns="/xml/ns/j2ee"

xmlns:xsi="/2001/XMLSchema-instance"

xsi:schemaLocation="/xml/ns/j2ee

/xml/ns/j2ee/web-app_2_4.xsd">

<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>RegisterServlet</servlet-name>

<servlet-class>com.michael.servletform.RegisterServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>RegisterServlet</servlet-name>

<url-pattern>/servlet/RegisterServlet</url-pattern>

</servlet-mapping>

</web-app>测试

–读取多个表单

• String[] hobby = request.getParameterValues(“hobby”);register.html<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>register.html</title>

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="this is my page">

<meta http-equiv="content-type" content="text/html; charset=UTF-8">

<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

</head>

<body>

<form action="/Servlet_Form/servlet/RegisterServlet" method="post">

用户名称:<input type="text" name="username"><br>

用户密码:<input type="password" name="password"><br>

用户爱好:<input type="checkbox" name="hobby" value="1">游泳

<input type="checkbox" name="hobby" value="2">足球<br>

<input type="submit" value="注册">

</body>

</html>RegisterServlet.javapackage com.michael.servletform;

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;

public class RegisterServlet extends HttpServlet {

/**

* Constructor of the object.

*/

public RegisterServlet() {

super();

}

/**

* Destruction of the servlet. <br>

*/

public void destroy() {

super.destroy(); // Just puts "destroy" string in log

// Put your code here

}

/**

* The doGet method of the servlet. <br>

*

* This method is called when a form has its tag value method equals to get.

*

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

String username = request.getParameter("username");

String password = request.getParameter("password");

String[] hobby = request.getParameterValues("hobby");

if(hobby!=null&&hobby.length>0){

for(int i=0;i<hobby.length;i++){

System.out.println(hobby[i]);

}

}

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out

.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");

out.println("<HTML>");

out.println("<HEAD><TITLE>A Servlet</TITLE></HEAD>");

out.println("<BODY>");

out.println("username:"+username);

out.println("password:"+password);

out.println("</BODY>");

out.println("</HTML>");

out.flush();

out.close();

}

/**

* The doPost method of the servlet. <br>

*

* This method is called when a form has its tag value method equals to post.

*

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doGet(request,response);

}

/**

* Initialization of the servlet. <br>

*

* @throws ServletException if an error occure

*/

public void init() throws ServletException {

// Put your code here

}

}测试一下hobby输出到控制台了 ##########Michael分割线################

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。