0

我正在尝试编写一个可以通过调用某个类的成员函数来打印的 HTML 代码,但我总是收到以下错误

    HTTP Status 500 - java.lang.NullPointerException

--------------------------------------------------------------------------------

type Exception report

message java.lang.NullPointerException

description The server encountered an internal error that prevented it from fulfilling this request.

exception 

org.apache.jasper.JasperException: java.lang.NullPointerException
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:96)


root cause 

java.lang.NullPointerException
    com.ly.bogha.oneworld.Pagemarkup.eBirth(Pagemarkup.java:59)
    org.apache.jsp.eHealth_jsp._jspService(eHealth_jsp.java:107)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:96)


note The full stack trace of the root cause is available in the Apache Tomcat/7.0.42 logs.

我的课是这个

package com.ly.bogha.oneworld;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;

public class Pagemarkup extends ActionSupport implements ServletResponseAware, ServletRequestAware  {

    protected HttpServletResponse mServletResponse;  
    protected HttpServletRequest mServletRequest;  

    public Pagemarkup() {
    }

    public void setHeader() throws IOException {

    }

    public void setBody() {

    }
    public void setFooter() {

    }
    @Override
    public void setServletRequest(HttpServletRequest servletRequest) {
        this.mServletRequest = servletRequest; 

    }

    @Override
    public void setServletResponse(HttpServletResponse servletResponse) {
        this.mServletResponse = servletResponse;  

    }
    public void eBirth() throws IOException{
        mServletResponse.setContentType("text/html");
        PrintWriter out = mServletResponse.getWriter();
        out.print("<table><tr><td>ebirth</td></tr></table>");

    }   

}

我正在调用的 JSP 文件就是这个

<%@page import="com.ly.bogha.oneworld.Pagemarkup"%>
<%@ page language="java" contentType="text/html; charset=windows-1256"
    pageEncoding="windows-1256"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
    content="text/html; charset=windows-1256">
<title>Insert title here</title>
</head>
<body>
    <jsp:include page="header.html"></jsp:include>
    <%
        Cookie[] cookies = request.getCookies();
        int ii = 0;
        boolean found = false;
        String[] error = { "Authirization required to Access the Page, Please login." };
        for (int i = 0; i < cookies.length; i++) {
            if (cookies[i].getName().equals("nid")) {
                found = true;
                ii = i;

            }
        }

        if (found == true) {
    %>
    <jsp:include page="ehealth.html"></jsp:include>
    <%

            if (request.getParameter("s") != null) {
                Pagemarkup ebirth = new Pagemarkup();
                int submenu = Integer.parseInt(request.getParameter("s"));
                switch (submenu) {
                case 1:
                    ebirth.eBirth();
                    break;
                case 2:

                    break;
                case 3:

                    break;
                }

            }
        } else {
            response.sendRedirect("login.jsp?msg=" + error[0]);
        }
    %>
</body>

当我的程序在 JSP 文件中执行以下代码时出现错误

ebirth.eBirth();
4

1 回答 1

1

我无法清楚地理解您的问题,即您如何在 struts2 中使用您的动作类。

但是我认为,当您在 jsp 中创建类Pagemarkup的实例时,以及当您调用函数 eBirth 时,那时您的mServletResponse将为空,因为在调用操作后设置了 servlet 响应。你正在创建一个实例。并不是说您必须在 xml 映射中定义的操作已执行。

您应该参考这个,它可能会有所帮助并帮助您理解相同的内容: http: //struts.apache.org/docs/how-can-we-access-the-httpservletresponse.html

在执行动作类或执行您的映射方法后返回响应。或者,如果您想这样,您可以从 eBirth 方法返回您的 html。你可以得到你想要的结果,但它不会去如何使用 struts2 框架。回答任何问题。

于 2017-07-26T08:35:58.927 回答