我在servlet中的request竟然失效了(100分)

  • 主题发起人 蓝色虾
  • 开始时间

蓝色虾

Unregistered / Unconfirmed
GUEST, unregistred user!
凡是关于用request取得值都是null,竟然连request.getAuthType()也为null,
连本机的ip地址也为null,弄了半天都不行,我真是没办法了,
请各位大虾帮我看看代码好吗?环境为jbuilder7。
代码:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.SQLException;
import com.jspsmart.upload.*;
public class HtmlBuilder extends HttpServlet {
static final private String CONTENT_TYPE = "text/html;
charset=GB2312";
private String problemIDFile;
private String jdbcDriver;
private String databaseURL;
private String appDir;
private String appHTMLDir;
private String appHTML2DDir;
private String appHTML3DDir;
private String appHTML2DReDir;
private String appHTML3DReDir;
private ServletContext context;
private ServletConfig config;
//Initialize global variables
public void init() throws ServletException{
try{
context=getServletContext();
config=getServletConfig();
this.init(context);
}catch(ServletException e){e.printStackTrace();}
}
/**
* Initializes
*/
public void init(ServletContext context)
throws ServletException
{
// and the problem ID assignment file
String problemIDFile =context.getInitParameter("problemIDFile");
if (problemIDFile == null)
throw new ServletException("No problemIDFile property specified");
setProblemIDFile(problemIDFile);
//设置程序目录
String appDir =context.getInitParameter("appDir");
if (appDir == null)
throw new ServletException("No appDir property specified");
setAppDir(appDir);
//设置HTML文件存放目录
String appHTMLDir =context.getInitParameter("appHTMLDir");
//测试代码
System.out.println("HTML文件所在的目录为:"+appHTMLDir);
if (appHTMLDir == null)
throw new ServletException("No appHTMLDir property specified");
setAppHTMLDir(appHTMLDir);
String appHTML2DReDir=context.getInitParameter("appHTML2DReDir");
if (appHTML2DReDir == null)
throw new ServletException("No appHTML2DReDir property specified");
setAppHTML2DReDir(appHTML2DReDir);
String appHTML3DReDir=context.getInitParameter("appHTML3DReDir");
if (appHTML3DReDir == null)
throw new ServletException("No appHTML3DReDir property specified");
setAppHTML3DReDir(appHTML3DReDir);
}
/**
* Returns the problemIDFile.
*/
public String getProblemIDFile()
{
return problemIDFile;
}
/**
* Sets the problemIDFile.
* @param problemIDFile the problemIDFile.
*/
public void setProblemIDFile(String problemIDFile)
{
this.problemIDFile = problemIDFile;
}
/**
* 设置程序所在的目录
*/
public void setAppDir(String dir){
this.appDir=dir;
}
/**
* 取得程序所在的目录
*/
public String getAppDir(){
return appDir;
}
/**
* 设置存放HTML文件的目录
*/
public void setAppHTMLDir(String htmldir){
this.appHTMLDir=htmldir;
}
/**
* 得到存放HTML文件的目录
*/
public String getAppHTMLDir(){
return appHTMLDir;
}
/**
* 设置存放2DHTML文件的目录
*/
public void setAppHTML2DDir(String htmldir){
this.appHTML2DDir=htmldir;
}
/**
* 得到存放2DHTML文件的目录
*/
public String getAppHTML2DDir(){
return appHTML2DDir;
}
/**
* 设置存放3DHTML文件的目录
*/
public void setAppHTML3DDir(String htmldir){
this.appHTML3DDir=htmldir;
}
/**
* 得到存放3DHTML文件的目录
*/
public String getAppHTML3DDir(){
return appHTML3DDir;
}
/**
* 设置存放2DHTML文件的相对目录
*/
public void setAppHTML2DReDir(String htmldir){
this.appHTML2DReDir=htmldir;
}
/**
* 得到存放2DHTML文件的相对目录
*/
public String getAppHTML2DReDir(){
return appHTML2DReDir;
}
/**
* 设置存放3DHTML文件的相对目录
*/
public void setAppHTML3DReDir(String htmldir){
this.appHTML3DReDir=htmldir;
}
/**
* 得到存放3DHTML文件的目录
*/
public String getAppHTML3DReDir(){
return appHTML3DReDir;
}
//Process the HTTP Get request
public voiddo
Get(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
this.doPost(request,response);
}
//Process the HTTP Post request
public voiddo
Post(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("验证类型:"+request.getAuthType());
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
SmartUpload mySmartUpload = new SmartUpload();
// Initialization
mySmartUpload.initialize(config,request,response);
TwoLib twolib=new TwoLib();
ThreeLib threelib=new ThreeLib();
ConnDateBase conndatebase=new ConnDateBase();
String id;
String subject;//标题
String author;//作者
String body;//内容
String smallPicname;//缩略图
String sort;//类别
String key;//关键字
String hasFile;//是否有相关文件
String[] fileName=new String[5];//相关文件的名字
String className;//总类别别,例:2D,3D
String date;//文件创建时间
String sql;//将文章标题等插入数据库的Sql语句
id=Util.assignProblemID(problemIDFile);
//测试代码
System.out.println("文章ID为:"+id);
subject=mySmartUpload.getRequest().getParameter("subject");
//测试代码
System.out.println("文章标题为:"+subject);
author=mySmartUpload.getRequest().getParameter("author");
sort=mySmartUpload.getRequest().getParameter("sort");
key=mySmartUpload.getRequest().getParameter("key");
date=Util.dateFormat(new Date());
twolib.setId(id);
twolib.setSubject(subject);
twolib.setAuthor(author);
twolib.setSort(sort);
twolib.setKey(key);
twolib.setDate(date);
body=mySmartUpload.getRequest().getParameter("body");
className=mySmartUpload.getRequest().getParameter("className");
//测试代码
System.out.println("总类别为:"+className);
hasFile=mySmartUpload.getRequest().getParameter("hasFile");
String bHTMLDirName;//存储HTML的文件夹名
String bHTMLFileName;//HTML的文件名
String bHTMLFileDir;//存储HTML的相对于根目录的文件夹名
String htmlPath;//存储HTML相对于defaultroot的HTML文件名
bHTMLDirName=Util.getBHTMLDirName();
bHTMLFileName=bHTMLDirName+".htm";//HTML的文件名以目录文件名+.htm
if(className.equalsIgnoreCase("2d")){
bHTMLFileDir=appHTML2DDir+bHTMLDirName;
htmlPath=appHTML2DReDir+bHTMLFileName;
twolib.setHtmlPath(htmlPath);
}else
{
bHTMLFileDir=appHTML3DDir+bHTMLDirName;
htmlPath=appHTML3DReDir+bHTMLFileName;
}
//生成存储HTML的文件夹
BuildDir builddir=new BuildDir(appHTMLDir,bHTMLDirName);
builddir.create();
try {
if(hasFile.equals("0")){
// Upload
mySmartUpload.upload();
// Save the file with the original name
// in a virtual path of the web server
mySmartUpload.save(bHTMLFileDir,mySmartUpload.SAVE_PHYSICAL);
}else
{
for (int i=0;i<mySmartUpload.getFiles().getCount();i++){
// Retreive the current file
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
// Save it only if this file exists
if (!myFile.isMissing()) {
// Save the files with its original names in a virtual path of the web server
myFile.saveAs(bHTMLFileDir + myFile.getFileName(), mySmartUpload.SAVE_PHYSICAL);
//将上传的相关文件的名字读出
fileName=myFile.getFieldName();//最后一个上传的为相关文件,所以这个fileName应该为相关文件的文件名
}
}
}
twolib.setSmallPicname(fileName[0]);
twolib.setDownFileName(fileName[1]);
} catch (Exception e){
out.println("Unable to upload the file.<br>");
out.println("Error : " + e.toString());
}
//将文章的作者等插入数据库

if(className.equalsIgnoreCase("2d")){
try{
conndatebase.init(getServletContext());
conndatebase.goSql(twolib);
}catch(SQLException e){e.printStackTrace();}
}else
{
}

}
//Clean up resources
public void destroy() {
}
}
 
原来用SmartUpload上传文件,必须在执行了upload()方法后才能够用request取得参数。
我没有细心分析它的sample
 
返还分数给楼主。
 
返还分数给楼主。
 
顶部