bogha
bogha

Reputation: 551

Write HTML from Inside Struts2 Java Class

I'm trying to write a HTML code that can be printed by calling a member function of some class but I get the following error always

    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.

My Class is this

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>");

    }   

}

and JSP file from where I'm calling is this

<%@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>

I get the error when my program executes the following code in the JSP file

ebirth.eBirth();

Upvotes: 0

Views: 696

Answers (1)

atishaya11
atishaya11

Reputation: 66

I am not able to clearly understand your problem, that is, how you are working with your action class in struts2.

But I think when you create instance of your class Pagemarkup in the jsp, and when you call the function eBirth, at that time your mServletResponse would be null as the servlet response is set after the action is called. And you are creating an instance. Not that your action is executed which you must have defined in your xml mapping.

You should refer to this, it might be helpful and help you understand the same: http://struts.apache.org/docs/how-can-we-access-the-httpservletresponse.html

The response is returned after the action class execute or your mapped method is executed. OR If you want to this way you can return your html from eBirth method. You can get your desired result but it will not go how struts2 framework is used. Do reply for any queries.

Upvotes: 1

Related Questions