我在Java 1.6中有一个应用程序,我想将其升级到Java 1.8,但是问题是,同时将jsp中的某些变量传输到servlet时,给出了 null 。它可以在Java 1.6上正常工作。
jsp代码:
Random rdm=new Random();
int rl=rdm.nextInt();
String hash1 = Integer.toHexString(rl);
String capstr=hash1.substring(0,4);
session.setAttribute("key",capstr);
这是我的servlet代码:
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class StrutsUploadAndSaveAction extends org.apache.struts.action.Action {
private final static String SUCCESS = "success";
private final static String FAILURE = "failure";
@Override
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{
StrutsUploadAndSaveForm myForm = (StrutsUploadAndSaveForm)form;
String Name = myForm.getUid();
String Password = myForm.getHash();
HttpSession session = request.getSession(true);
session.setAttribute("Name",myForm.getUid());
session.setAttribute("Password",myForm.getHash());
String key=(String)session.getAttribute("key");
System.out.println("login page Action captcha Keys"+key);
String user = request.getParameter("number");
System.out.println(" login page Action captcha User"+user);
if(key.compareTo(user)==0)
{
return mapping.findForward("success");
}
else{
return mapping.findForward("failure");
}}}
,错误是:
类型异常报告
消息java.lang.NullPointerException
说明服务器遇到内部错误,导致服务器无法满足此请求。
例外
javax.servlet.ServletException:java.lang.NullPointerException org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:520) org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:427) org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:228) org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913) org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462) javax.servlet.http.HttpServlet.service(HttpServlet.java:647) javax.servlet.http.HttpServlet.service(HttpServlet.java:728) org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
根本原因
java.lang.NullPointerException roseindia.net.StrutsUploadAndSaveAction.execute(StrutsUploadAndSaveAction.java:68) org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:425) org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:228) org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913) org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462) javax.servlet.http.HttpServlet.service(HttpServlet.java:647) javax.servlet.http.HttpServlet.service(HttpServlet.java:728)