编写一个表单页面census.html,让用户填写姓名、性别(男女选择)、兴趣(运动,读书,音乐,书法及其他)

编写一个表单页面census.html,让用户填写姓名、性别(男女选择)、兴趣(运动,读书,音乐,书法及其他),第1张

表单

<form action="doservlet" method="post">

姓名:<input name="user_name"><br>

性别<input type="radio" value="man" name="sex">男 <input type="radio" value="women" name="sex">女<Br />

爱好:<input type="check" name="likes" value="运动" />运动

<input type="check" name="likes" value="读书" />读书

<input type="check" name="likes" value="音乐" />音乐

<input type="check" name="likes" value="书法" />书法

</form>

servlet:

iimport 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 doservlet extends HttpServlet {

/**

* Constructor of the object.

*/

public doservlet() {

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 {

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.print("This is ")

out.print(this.getClass())

out.println(", using the GET method")

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 {

response.setContentType("text/html")

PrintWriter out = response.getWriter()

String name = null

String sex = null

String likes[] = null

name=request.getParameter("user_name")

sex=request.getParameter("sex")

likes=request.getParameterValues("likes")

request.getSession().setAttribute("user_name", name)

request.getSession().setAttribute("sex", sex)

request.getSession().setAttribute("like",likes)

//把请求过来的数据放在session里

response.sendRedirect("目的页")

//在目的页中通过session的 getAttribute方法取出来即可

out.flush()

out.close()

}

/**

* Initialization of the servlet. <br>

*

* @throws ServletException if an error occure

*/

public void init() throws ServletException {

// Put your code here

}

}

。。。。。。。。

这个也太过简单了吧

可惜来迟了

被人抢先了

还是贴一遍

(1)action

(2)submit

(3)form

不过这个表单还是有问题,结构有问题

应该是

<html>

<head></head>

<body>这中间加你的结构</body>

</html>

我们在浏览一些网站时经常会用到一种网页元素--表单。如下图,像这样允许用户输入并使用按钮提交信息的功能,我们称之为表单,那么接下来介绍HTML表单相关的一些知识点。

1)表单:帮助服务器收集客户端信息的一种机制

<form action="提交到的页面" method="提交的方法">

表单控件

form>

action:数据最后所提交的页面 绝对路径/相对路径

method:数据提交服务器时的提交方法 post get

2)表单控件:表单用于用户填写信息的控件

1、文本控件: 用于用户填写较少信息,且可以显示用户填写信息的控件

(注册时,用户名填写)

格式:

<input type="text" name=""  id=""  value="" size="" placeholder=""/>

type:设置控件样式 text

name:用于表单收集信息的一种标示符,也就是控件的名称

2、密码框:只是用户输入的内容看不到

<input type="password" name="" id=""  size=""/>

3、单选按钮组:由多个单选按钮组成的按钮组,只能选中其中一个,这种按钮组

<input type='radio' name="" value="" checked>标示符

name:为了保证传递服务器数据只有一个,所以name值必须相同

value:是用提交服务器时,该标签被提交的数据,value不能相同

checked:默认按钮组中的那个按钮被选中

性别:<input type='radio' name="" value="" checked>男

<input type='radio' name="" value="" checked>女

<input type='radio' name="" value="" checked>保密

所有表单控件name值不能相同!!!

4、复选按钮组: 与单选按钮组的区别可以选择多个,为了能将用户选择的多项内容传递服务器中,要将name名字后面加个[]

格式:

<input type='checkbox' name="" id="" value="" checked>标示

选课:

<input type='checkbox' name='classes[]' checked value='1'>语文

<input type='checkbox' name='classes[]'  value='2'>数学

<input type='checkbox' name='classes[]'  value='3'>历史

<input type='checkbox' name='classes[]'  value='4'>政治

<input type='checkbox' name='classes[]'  value='5'>物理

<input type='checkbox' name='classes[]'  value='6'>化学

<input type='checkbox' name='classes[]' checked value='7'>美术

注意:一定要在name属性后面添加[]

5)下拉列表框:

格式:

<select name="" >

<option value="" selected>下拉列表项option>

<option value="" >下拉列表项option>

<option value="" >下拉列表项option>

<option value="" >下拉列表项option>

<option value="" >下拉列表项option>

select>

multiple:允许用户选中多个下拉列表项

optgroup: 设置下拉分类标示

6) 文本域:可以输入多行数据的表单控件

<textarea name="" id="" cols="" rows="">

内容

textarea>

rows:设置文本域可以输入的行数

cols:设置的文本域输入的列数(一行可以输入多少个字符)(不是PX)

个人介绍:

<textarea name='descs' cols="50" rows="10">

我是个学生

textarea>

7)隐藏域:

<input type="hidden" name = “”value =””>

8)上传控件:

<input type="file" name=””>

9)按钮标签:

普通按钮:为js提供的控件

<input type='button' name=”” value=""/>

value:按钮中显示的数据和提交到服务器后该控件提交的值

<input type='button' name='clicks' value="点我"/>

提交按钮:点击后可以提交表单

<input type='submit' name="add" value='注册'/>

重置按钮:点击后可以将用户输入的数据清空

<input type='reset' name='cancel' value='重置'>

图片提交按钮:使用图片作为提交按钮

<input type='image' src='图片地址' name=””>

本文的分享就到这,希望我的回答能帮到你。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/7628867.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-08
下一篇2023-04-08

发表评论

登录后才能评论

评论列表(0条)

    保存