当前位置: 技术问答>java相关
我在servlet中的request竟然失效了
来源: 互联网 发布时间:2015-08-07
本文导语: 凡是关于用request取得值都是null,竟然连request.getAuthType()也为null,去本机的ip地址也为null,弄了半天都不行,我真是没办法了,请各位大虾帮我看看代码好吗? package com.sparkwater.model; import javax.servlet.*; import javax.servlet...
凡是关于用request取得值都是null,竟然连request.getAuthType()也为null,去本机的ip地址也为null,弄了半天都不行,我真是没办法了,请各位大虾帮我看看代码好吗?
package com.sparkwater.model;
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;
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);
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;
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
this.doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("验证类型:"+request.getAuthType());//竟然为null
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;
}
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
package com.sparkwater.model;
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;
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);
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;
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
this.doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("验证类型:"+request.getAuthType());//竟然为null
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;
}
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