当前位置: 技术问答>java相关
用Java Servlet作一个表单的提交程序,可是调用时出错了?应该怎么解决啊?
来源: 互联网 发布时间:2015-01-21
本文导语: 我用Java Servlet作一个表单的提交程序,要将表单中的值提取后显示出来,在运行时出现如下错误 Error: 500 Location: /examples/servlet/ParmExtractorServlet Internal Servlet Error: java.lang.NullPointerException at java.lang.ClassLoader.r...
我用Java Servlet作一个表单的提交程序,要将表单中的值提取后显示出来,在运行时出现如下错误
Error: 500
Location: /examples/servlet/ParmExtractorServlet
Internal Servlet Error:
java.lang.NullPointerException
at java.lang.ClassLoader.resolveClass0(Native Method)
at java.lang.ClassLoader.resolveClass(ClassLoader.java:588)
at org.apache.tomcat.loader.AdaptiveClassLoader.loadClass(AdaptiveClassLoader.java:430)
at org.apache.tomcat.loader.AdaptiveServletLoader.loadClass(AdaptiveServletLoader.java:174)
at org.apache.tomcat.core.ServletWrapper.loadServlet(ServletWrapper.java:265)
at org.apache.tomcat.core.ServletWrapper.init(ServletWrapper.java:289)
at org.apache.tomcat.core.Handler.service(Handler.java:254)
at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:797)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java:743)
at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:210)
at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416)
at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:498)
at java.lang.Thread.run(Thread.java:484)
源程序如下:
package ijs.forms;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ParmExtractorServlet extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
response.setContentType("text/plain");
ServletOutputStream out=response.getOutputStream();
Enumeration eParmNames=request.getParameterNames();
while(eParmNames.hasMoreElements())
{
String name=(String)eParmNames.nextElement();
String value=request.getParameter(name);
out.println(name+"="+value);
}
}
}
Error: 500
Location: /examples/servlet/ParmExtractorServlet
Internal Servlet Error:
java.lang.NullPointerException
at java.lang.ClassLoader.resolveClass0(Native Method)
at java.lang.ClassLoader.resolveClass(ClassLoader.java:588)
at org.apache.tomcat.loader.AdaptiveClassLoader.loadClass(AdaptiveClassLoader.java:430)
at org.apache.tomcat.loader.AdaptiveServletLoader.loadClass(AdaptiveServletLoader.java:174)
at org.apache.tomcat.core.ServletWrapper.loadServlet(ServletWrapper.java:265)
at org.apache.tomcat.core.ServletWrapper.init(ServletWrapper.java:289)
at org.apache.tomcat.core.Handler.service(Handler.java:254)
at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:797)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java:743)
at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:210)
at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416)
at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:498)
at java.lang.Thread.run(Thread.java:484)
源程序如下:
package ijs.forms;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ParmExtractorServlet extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
response.setContentType("text/plain");
ServletOutputStream out=response.getOutputStream();
Enumeration eParmNames=request.getParameterNames();
while(eParmNames.hasMoreElements())
{
String name=(String)eParmNames.nextElement();
String value=request.getParameter(name);
out.println(name+"="+value);
}
}
}
|
我觉得是这样:
String value=null;
value = request.getParameter(name);
if(value != null){
out.println(name+"="+value);
}
在调用变量之前作个判断
String value=null;
value = request.getParameter(name);
if(value != null){
out.println(name+"="+value);
}
在调用变量之前作个判断
|
System.out.println(),再输出字串时,实际上是调用对象的一个从object类继承来的方法:toString(),但是你的对象不存在,所以会有:NullPointerException
你应该这样:
1.不管什么情况下,都尽可能初始化对象。
如:String name = "";
String name = xxxxxxxx;
2.使用前进行处理:
如:if (name == null ) {
name = "";
} else {
name = xxxxxx;
}
你应该这样:
1.不管什么情况下,都尽可能初始化对象。
如:String name = "";
String name = xxxxxxxx;
2.使用前进行处理:
如:if (name == null ) {
name = "";
} else {
name = xxxxxx;
}
|
form的action会不会应该是"../servlet/ijs.forms.ParmExtractorServlet",:)
|
Agla,我知道了,你应该把编译好的class文件放到C:tomcatwebappsexamplesWEB-INFclassesijsforms目录下,然后在调用的时候url应该为/examples/servlet/ijs.forms.ParmExtractorServlet。