|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
在ruby里才是一切皆对象。当然我不并不是很了解ruby,但是ruby确实是将语法简化得很好。js|servlet|显现续
---
/**
*Insertthemethodsdescriptionhere.
*Creationdate:(2001-2-417:28:17)
*@returnjava.util.Enumeration
*/
publicEnumerationgetParameterNames(){
if(pairs==null)returnnull;
returnpairs.keys();
}
/**
*Insertthemethodsdescriptionhere.
*Creationdate:(2001-2-417:33:40)
*@returnjava.lang.String[]
*@paramnamejava.lang.String
*/
publicString[]getParameterValues(Stringname){
if(pairs==null||!pairs.containsKey(name))returnnull;
ArrayListal=(ArrayList)pairs.get(name);
String[]values=newString[al.size()];
for(inti=0;i<values.length;i++)
values[i]=(String)al.get(i);
returnvalues;
}
/**
*Insertthemethodsdescriptionhere.
*Creationdate:(2001-2-420:34:37)
*@paramurlencjava.lang.String
*/
privatevoidparse(Stringurlenc)throwsjava.io.IOException{
if(urlenc==null)return;
StringTokenizertok=newStringTokenizer(urlenc,"&");
try{
while(tok.hasMoreTokens()){
StringaPair=tok.nextToken();
intpos=aPair.indexOf("=");
Stringname=null;
Stringvalue=null;
if(pos!=-1){
name=decode(aPair.substring(0,pos));
value=decode(aPair.substring(pos+1));
}else{
name=aPair;
value="";
}
if(pairs.containsKey(name)){
ArrayListvalues=(ArrayList)pairs.get(name);
values.add(value);
}else{
ArrayListvalues=newArrayList();
values.add(value);
pairs.put(name,values);
}
}
}catch(Exceptione){
thrownewjava.io.IOException(e.getMessage());
}
}
/**
*Insertthemethodsdescriptionhere.
*Creationdate:(2001-2-420:34:37)
*@paramurlencjava.lang.String
*/
privatevoidparse(Stringurlenc,Stringencoding)throwsjava.io.IOException{
if(urlenc==null)return;
StringTokenizertok=newStringTokenizer(urlenc,"&");
try{
while(tok.hasMoreTokens()){
StringaPair=tok.nextToken();
intpos=aPair.indexOf("=");
Stringname=null;
Stringvalue=null;
if(pos!=-1){
name=decode(aPair.substring(0,pos),encoding);
value=decode(aPair.substring(pos+1),encoding);
}else{
name=aPair;
value="";
}
if(pairs.containsKey(name)){
ArrayListvalues=(ArrayList)pairs.get(name);
values.add(value);
}else{
ArrayListvalues=newArrayList();
values.add(value);
pairs.put(name,values);
}
}
}catch(Exceptione){
thrownewjava.io.IOException(e.getMessage());
}
}
}
这个类的功效就是读取并保留form提交的信息,并完成经常使用的getParameter办法。
packagecom.hto.servlet;
importjava.io.*;
importjavax.servlet.*;
importjavax.servlet.http.*;
/**
*Insertthetypesdescriptionhere.
*Creationdate:(2001-2-58:28:20)
*@author:钱卫春
*/
publicclassUtfBaseServletextendsHttpServlet{
publicstaticfinalStringPARAMS_ATTR_NAME="PARAMS_ATTR_NAME";
/**
*ProcessincomingHTTPGETrequests
*
*@paramrequestObjectthatencapsulatestherequesttotheservlet
*@paramresponseObjectthatencapsulatestheresponsefromtheservlet
*/
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
performTask(request,response);
}
/**
*ProcessincomingHTTPPOSTrequests
*
*@paramrequestObjectthatencapsulatestherequesttotheservlet
*@paramresponseObjectthatencapsulatestheresponsefromtheservlet
*/
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
performTask(request,response);
}
/**
*Insertthemethodsdescriptionhere.
*Creationdate:(2001-2-58:52:43)
*@returnint
*@paramrequestjavax.servlet.http.HttpServletRequest
*@paramnamejava.lang.String
*@paramrequiredboolean
*@paramdefValueint
*/
publicstaticjava.sql.DategetDateParameter(HttpServletRequestrequest,Stringname,booleanrequired,java.sql.DatedefValue)throwsServletException{
Stringvalue=getParameter(request,name,required,String.valueOf(defValue));
returnjava.sql.Date.valueOf(value);
}
/**
*Insertthemethodsdescriptionhere.
*Creationdate:(2001-2-58:52:43)
*@returnint
*@paramrequestjavax.servlet.http.HttpServletRequest
*@paramnamejava.lang.String
*@paramrequiredboolean
*@paramdefValueint
*/
publicstaticdoublegetDoubleParameter(HttpServletRequestrequest,Stringname,booleanrequired,doubledefValue)throwsServletException{
Stringvalue=getParameter(request,name,required,String.valueOf(defValue));
returnDouble.parseDouble(value);
}
/**
*Insertthemethodsdescriptionhere.
*Creationdate:(2001-2-58:52:43)
*@returnint
*@paramrequestjavax.servlet.http.HttpServletRequest
*@paramnamejava.lang.String
*@paramrequiredboolean
*@paramdefValueint
*/
publicstaticfloatgetFloatParameter(HttpServletRequestrequest,Stringname,booleanrequired,floatdefValue)throwsServletException{
Stringvalue=getParameter(request,name,required,String.valueOf(defValue));
returnFloat.parseFloat(value);
}
/**
*Insertthemethodsdescriptionhere.
*Creationdate:(2001-2-58:52:43)
*@returnint
*@paramrequestjavax.servlet.http.HttpServletRequest
*@paramnamejava.lang.String
*@paramrequiredboolean
*@paramdefValueint
*/
publicstaticintgetIntParameter(HttpServletRequestrequest,Stringname,booleanrequired,intdefValue)throwsServletException{
Stringvalue=getParameter(request,name,required,String.valueOf(defValue));
returnInteger.parseInt(value);
}
/**
*Insertthemethodsdescriptionhere.
*Creationdate:(2001-2-58:43:36)
*@returnjava.lang.String
*@paramrequestjavax.servlet.http.HttpServletRequest
*@paramnamejava.lang.String
*@paramrequiredboolean
*@paramdefValuejava.lang.String
*/
publicstaticStringgetParameter(HttpServletRequestrequest,Stringname,booleanrequired,StringdefValue)throwsServletException{
if(request.getAttribute(UtfBaseServlet.PARAMS_ATTR_NAME)!=null){
UTF8ParameterReaderparams=(UTF8ParameterReader)request.getAttribute(UtfBaseServlet.PARAMS_ATTR_NAME);
if(params.getParameter(name)!=null)returnparams.getParameter(name);
if(required)thrownewServletException("TheParameter"+name+"Requiredbutnotprovided!");
elsereturndefValue;
}else{
if(request.getParameter(name)!=null)returnrequest.getParameter(name);
if(required)thrownewServletException("TheParameter"+name+"Requiredbutnotprovided!");
elsereturndefValue;
}
}
/**
*Returnstheservletinfostring.
*/
publicStringgetServletInfo(){
returnsuper.getServletInfo();
}
/**
*Insertthemethodsdescriptionhere.
*Creationdate:(2001-2-58:52:43)
*@returnint
*@paramrequestjavax.servlet.http.HttpServletRequest
*@paramnamejava.lang.String
*@paramrequiredboolean
*@paramdefValueint
*/
publicstaticjava.sql.TimestampgetTimestampParameter(HttpServletRequestrequest,Stringname,booleanrequired,java.sql.TimestampdefValue)throwsServletException{
Stringvalue=getParameter(request,name,required,String.valueOf(defValue));
returnjava.sql.Timestamp.valueOf(value);
}
/**
*Initializestheservlet.
*/
publicvoidinit(){
//insertcodetoinitializetheservlethere
}
/**
*Processincomingrequestsforinformation
*
*@paramrequestObjectthatencapsulatestherequesttotheservlet
*@paramresponseObjectthatencapsulatestheresponsefromtheservlet
*/
publicvoidperformTask(HttpServletRequestrequest,HttpServletResponseresponse){
try
{
//Insertusercodefromhere.
}
catch(ThrowabletheException)
{
//uncommentthefollowinglinewhenunexpectedexceptions
//areoccuringtoaidindebuggingtheproblem.
//theException.printStackTrace();
}
}
/**
*Insertthemethodsdescriptionhere.
*Creationdate:(2001-2-58:31:54)
*@paramrequestjavax.servlet.ServletRequest
*@paramresponsejavax.servlet.ServletResponse
*@exceptionjavax.servlet.ServletExceptionTheexceptiondescription.
*@exceptionjava.io.IOExceptionTheexceptiondescription.
*/
publicvoidservice(ServletRequestrequest,ServletResponseresponse)throwsjavax.servlet.ServletException,java.io.IOException{
Stringcontent=request.getContentType();
if(content==null||content!=null&&content.toLowerCase().startsWith("application/x-www-form-urlencoded"))
request.setAttribute(PARAMS_ATTR_NAME,newUTF8ParameterReader((HttpServletRequest)request));
super.service(request,response);
}
}
这个就是Servlet基类,它掩盖了父类的service办法,在挪用父类service前,创立了UTF8ParameterReader对象,个中保留了form中提交的信息。然后把这个对象作为一个Attribute保留到Request对象中。然后还是挪用父类的service办法。
关于承继这个类的Servlet,要注重的是,"尺度"getParameter在也不克不及读到post的数据,由于在这之前这个类中已从ServletInputStream中读出了数据了。以是应当利用该类中供应的getParameter办法。
剩下的就是输入成绩了,我们要把输入的信息,转为UTF8的二进制流输入。只需我们设置Content-Type时指定charset为UTF8,然后利用PrintWriter输入,那末这些转换是主动举行的,Servlet中如许设置:
response.setContentType("text/html;charset=UTF8");
Jsp中如许设置:
<%@pagecontentType="text/html;charset=UTF8"%>
如许就能够包管输入是UTF8流,客户端可否显现,就看客户真个了。
关于multipart/form-data的form提交的内容,我也供应一个类用来处置,在这个类的机关子中能够指定页面利用的charset,默许仍是UTF-8,限于篇幅不贴出源码,假如感乐趣能够mailto:vividq@china.com和我切磋。
但是我同意你的观点,对于大型项目来说,应该是采用框架的一部分,根据功能的不同而改进,欢迎你能再提出些宝贵意见,我会多多学习的。说到jbuilder,我可能是个人感觉,用的时候确实没有vs爽,我最喜欢的IDE是netbeans,谢谢。 |
|