锁链之亡灵 2015-12-02 01:23 采纳率: 50%
浏览 1399

未赋值的session在jsp报错,求大神帮帮忙吧

想要在页面打开是判定一个session会话,这个session要等到跳转到服务器之后才能赋值。
但是明明我设定了判断是否为空,可是还是报错。
这是我的页面和服务器代码:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<title>请填写上传表单</title>


<%
Object o = session.getAttribute("Error1");
if(!o.equals(null)||(!"".equals(o))){
String er = (String)o;
%>
    操作失败,<%=er %>
<%
}

%>


关键字:

简要描述(10个字,可以不写):

文章照片:






package com.hcpolic.servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.hcpolic.entity.Artical;
import com.hcpolic.entity.User;
import com.hcpolic.rbac.Opreator;
import com.hcpolic.rbac.Permission;
import com.hcpolic.util.Tools;

import org.apache.commons.fileupload.disk.*;
import org.apache.commons.fileupload.servlet.*;
import org.apache.commons.fileupload.*;

public class UploadServlet extends HttpServlet {

/**
 * The doGet method of the servlet. <br>
 *
 * This method is called when a form has its tag value method equals to get.
 * 
 * @param request the request send by the client to the server
 * @param response the response send by the server to the client
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    this.doPost(request, response);
}

/**
 * The doPost method of the servlet. <br>
 *
 * This method is called when a form has its tag value method equals to post.
 * 
 * @param request the request send by the client to the server
 * @param response the response send by the server to the client
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");
        HttpSession session=request.getSession();
        User us = (User)request.getSession().getAttribute("user");
        String rem = (String)request.getAttribute("remark");
        String key = (String)request.getAttribute("keyw");
        String er = null;
        Tools t = new Tools();
        ////////////////////////////////保存上传文件至指定文件夹
        String savePath = "D:/upload/";
        DiskFileItemFactory fac = new DiskFileItemFactory();
        fac.setRepository(new File(savePath+"uploadtemp"));
        ServletFileUpload upload = new ServletFileUpload(fac);
        upload.setFileSizeMax(1048576);
        try {
            List<FileItem> it = upload.parseRequest(request);
            Iterator<FileItem> iter = it.iterator();

            while(iter.hasNext()){
                FileItem item = iter.next();
                String fieldName = item.getFieldName();
                if(!item.isFormField()){
                    File saveFile = null;
                    InputStream input = null;
                    OutputStream output = null;
                    savePath = savePath + File.separator+String.valueOf(t.randomId(9,6))+"."+item.getName().split("\\.")[1];
                    input = item.getInputStream();
                    output = new FileOutputStream(new File(savePath));
                    byte data[] = new byte[512];
                    int temp = 0;
                    while((temp = input.read(data,0,512))!=-1){
                        output.write(data);
                    }
                    input.close();
                    output.close();
                }

            }
        } catch (FileUploadException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        ////////////////////////////////
        ///////////////////////////////抓取系统时间
        String strDate = null;
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        strDate = sf.format(new Date());
        ///////////////////////////////
        Artical a = new Artical();
        Permission per = new Permission();
        Opreator o = new Opreator();
        a.setId(us.getId().substring(4, 10)+String.valueOf(t.randomId(9,6)));
        a.setKeyWords(key.trim());
        a.setRemark(rem);
        a.setImagePath(savePath);
        a.setUpdateTime(strDate.trim());

        boolean ret = o.isAdd(a, us);
        if(ret){
            response.sendRedirect("main.jsp");
        }else{
            er = "您的权限不够!!";
            session.setAttribute("Error1", er);
            response.sendRedirect("Upload.jsp");
        }
}

}

  • 写回答

1条回答

  • GrowingDarker 2015-12-02 02:12
    关注

    谔谔。小伙不理解空指针怎么来啊。空指针 是指调用 null值的对象或方法。懂了吗?
    你那个o 的值 是 null 你调用 o的 equals 方法 等于 这么写 null.equals(); 当然会空指针。 判断是否为空就
    o == null 或 o != null 这样写就行了。

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作