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

能帮我回答一下嘛 。 发到470138325@QQ.com
上述表单提交给Servlet来处理该表单程序,并回送用户的填写结果。
要求调试并能够运行结果。显示结果为:
您的姓名是:
您的性别是:
您的兴趣爱好是:
根据census.html,利用Servlet的相应方法和web.xml配置,实现Form表单内容的读取。Servlet中的doGet()方法和doPost() 方法都要定义。要求写出census.html和Servlet文件和web.xml关于servlet的配置代码。

表单:
<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个回答  2011-08-29
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<form method="post" action="xx.asp">
<table width="100%" cellpadding="0" cellspacing="0" border="1px">
<tr align="center"><td>姓名:</td><td><input type="text" name="name" /></td></tr>
<tr align="center"><td>性别:</td><td><input type="radio" name="sex" value="男" checked="checked"/>男 <input type="radio" name="sex" value="女" /> 女</td></tr>
<tr align="center"><td>兴趣:</td><td><input type="checkbox" name="chk" value="运动" />运动 <input type="checkbox" name="chk" value="读书" />读书 <input type="checkbox" name="chk" value="音乐" />音乐 <input type="checkbox" name="chk" value="书法" />书法 <input type="checkbox" name="chk" value="其他" />其他</td></tr>
<tr align="center"><td colspan="2"><input type="submit" value="提交" /></td></tr>
</table>
</form>
</body>
</html>追问

- - 还有要求呢。

追答

额,java吗不会搞,那么就请楼下的回答你。

编写一个表单页面census.html,让用户填写姓名、性别(男女选择)、兴趣...
性别<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="音...

相似回答