Servlet (处理form表单)

 

Servlet这部分现在我们用到的其实就是处理form表单,以后要用到什么接口,类和方法具体学习。

1.。。。。第一个是对不同的选框的信息的接收:(具体的我加了注释)

package com;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class biaodan extends HttpServlet {

/**
* Constructor of the object.
*/
public biaodan() {
   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 {

   //接收参数
   //文本框
   PrintWriter out = response.getWriter();
   //String a=request.getParameter("body");
   //out.println(a);
  
   //单选框
// String b=request.getParameter("weather");
// out.println(b);
   //复选框
   Enumeration values=request.getParameterNames();
   while(values.hasMoreElements())
   {
    String name=(String)values.nextElement();
    String c=request.getParameterValues(name)[0];
    if(name.compareTo("submit")!=0)
    {
    out.println(c);
    }
   }
   //密码框
// String f=request.getParameter("pass");
// out.println(f);
  
//下拉列表
// String d=request.getParameter("guo");
// out.println(d);
  
   //文本域
// String e=request.getParameter("content");
// out.println(e);
   out.flush();
   out.close();
}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
   // Put your code here
}

}

 

 

2.。。。。。这个是我对一个具体的页面写的,接受参数:(我给了部分方法的的用法)在后面我给出了这个html页面的类容。

package com;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class diaocha extends HttpServlet {

/**
* Constructor of the object.
*/
public diaocha() {
   super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
   super.destroy(); // Just puts "destroy" string in log
   // Put your code here
}


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");
   //得到响应的 PrinterWriter以返回文本给客户端
   PrintWriter out = response.getWriter();
   String mystr="";
   //打开一个问件写入survey的结果
   String surveyName=request.getParameterValues("survey")[0];
   String resultDir="D:";
  
  
   FileWriter resultsFile=new FileWriter(resultDir+System.getProperty("File.separator")+
     surveyName+".txt", true);
  
   PrintWriter toFile=new PrintWriter(resultsFile);
   //从客户端得到标段数据&存储在这个文件中
   toFile.println("<BEGIN>");
   Enumeration values=request.getParameterNames();
   while(values.hasMoreElements())
   {
    String name=(String)values.nextElement();
    String value=request.getParameterValues(name)[0];
    if(name.compareTo("submit")!=0)
    {
     toFile.println(name+":"+value);
     mystr=mystr+name+":"+value+"<br>";
    }
   }
   toFile.println("<END>");
   //关闭文件
   resultsFile.close();
   //用一个thank you返回客户端
  
   out.println("<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.01 Transitional//EN/">");
   out.println("<HTML>");
   out.println(" <HEAD><TITLE>Thank you!</TITLE></HEAD>");
   out.println(" <BODY>");
   out.print("   thanks you for participating ");
   out.print("<p>"+mystr+"</p>");
// out.println(", using the POST method");
   out.println(" </BODY>");
   out.println("</HTML>");
   out.flush();
   out.close();
}


/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
   // Put your code here
}
}

/*
*
* public FileWriter(String fileName,
                  boolean append)
           throws IOException在给出文件名的情况下构造 FileWriter 对象,
           它具有指示是否挂起写入数据的 boolean 值。
          
参数:
fileName - 一个字符串,表示与系统有关的文件名。
append - 一个 boolean 值,如果为 true,则将数据写入文件末尾处,而不是写入文件开始处。

System类中的方法
getProperty(String key)
          获得指定键指示的系统属性
参数:
key - 系统属性的名称。
返回:
系统属性的字符串值,如果没有带有此键的属性,则返回 null。

File类中的方法:
separator
public static final String separator与系统有关的默认名称分隔符,出于方便考虑,
它被表示为一个字符串。此字符串只包含一个字符,即 separatorChar。

*

* compareTo
public int compareTo(String anotherString)按字典顺序比较两个字符串。
该比较基于字符串中各个字符的 Unicode 值。
将此 String 对象表示的字符序列与参数字符串所表示的字符序列进行比较。
如果按字典顺序此 String 对象在参数字符串之前,则比较结果为一个负整数。
如果按字典顺序此 String 对象位于参数字符串之后,则比较结果为一个正整数。
如果这两个字符串相等,则结果为 0;compareTo 只有在方法 equals(Object) 返回 true 时才返回 0。
这是字典排序的定义。如果这两个字符串不同,则要么它们在某个索引处具有不同的字符,
该索引对二者均为有效索引,要么它们的长度不同,或者同时具备上述两种情况。
如果它们在一个或多个索引位置上具有不同的字符,假设 k 是这类索引的最小值;
则按照 < 运算符确定的那个字符串在位置 k 上具有较小的值,其字典顺序在其他字符串之前。
这种情况下,compareTo 返回这两个字符串在位置 k 处的两个不同的 char 值,即值:

this.charAt(k)-anotherString.charAt(k)
如果它们没有不同的索引位置,则较短字符串在字典顺序上位于较长字符串的前面。
这种情况下,compareTo 返回这两个字符串长度的不同,即值:
this.length()-anotherString.length()
指定者:
接口 Comparable<String> 中的 compareTo
参数:
anotherString - 要比较的 String。
返回:
如果参数字符串等于此字符串,则返回 0 值;如果按字典顺序此字符串小于字符串参数,
则返回一个小于 0 的值;如果按字典顺序此字符串大于字符串参数,则返回一个大于 0 的值。

*/

 

 

3.。这是这个程序处理的页面的类容

调查表

性别: 男 女

国籍:

爱好: 篮球 排球 足球 游泳

简介:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值