简体   繁体   中英

Issues with Displaying information on JSP from servlet

I'm having a spot of bother with a uni assignment where I am trying to display data from a cassandra database on a JSP through a servlet. Basically I am getting a null pointer exception when I try to use get methods on the class storing a user's information for a profile page.

The code below is the part of the JSP that requests the servlet send an object that holds the current user's details. When looking in the debugger it seems it gets the username without any problems but the request for Profile Details returns an empty object whereas it should be populated by information from the database.

 <%
        String Username=null;

        LoggedIn lg = (LoggedIn) session.getAttribute("LoggedIn");
        Username = lg.getUsername();

        ProfileDetails pd= new ProfileDetails();
        pd = (ProfileDetails) request.getAttribute("Profile");


        if (pd!=null)
        {
        String forename= pd.getForename();
        String surname=pd.getSurname();
        String email=pd.getEmail();


        %>

This next segment of code is the method in the servlet which calls the method which returns an object populated with the user's details.

  protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
  //  processRequest(request, response);
    HttpSession session=request.getSession();

     LoggedIn lg = (LoggedIn)session.getAttribute("LoggedIn");
    String username=lg.getUsername();

    User us = new User();
    us.setCluster(cluster);
    ProfileDetails pd= new ProfileDetails();

    pd = us.GetUserDetails(pd, username);
    request.setAttribute("Profile", pd);

    RequestDispatcher rd = request.getRequestDispatcher("Profile.jsp");
    rd.forward(request, response);

}

I'm fairly sure that the issue I'm having is based in one of these two blocks of code but at the minute I'm stumped. So any help would be appreciated greatly.

If anyone is feeling really enthusiastic about helping here is a link to my project for cloning on GitHub https://github.com/BrodieRoss/Instagrim.git

Ps sorry if any of my explaining here sounds a bit like nonsense, I'm pretty new to web development.

You have to approach a more MVC version of your web app. I saw that you have written some getters (voids) but it is better if you write and some setters methods for this class.

Then you have not to forward the servlet to your jsp but redirect it using:

HttpServletResponse.sendRedirect(String Location)

or

HttpServletResponse.sendRedirect("/your/new/location.jsp")

Before redirecting you have to set the attributes you want to this object. For accessing these attributes you use the same variables that your are using on the class. For displaying them in a jsp file you have to use jstl lib. Check more here: https://www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm

This tutorial might help you: https://docs.oracle.com/cd/E15919_01/wlp.1032/e14254/developuserprofiles.htm

User us = new User(); I presume relates a "cluster" of information that is part of the server instance ?, of I see no variable declared or assignment call of any relevant form for "cluster" itself, though/and too (et tu) I would say also "database" (as you say/wrote) means the "server configuration running instance information and SERVER-environment-variables, globals etc" of the Java J2EE server in this does not have an extra configured set of "connections" to another server program (eg MySQL Potgre ...) requiring java drivers and connection pools associated.

Its a wonder it compiled, however, something may be a "static" variable and requires casting to a non static context to be fed to a method or variable. Can you look through that better now?

The information would be better fed through JSP beans with some state (just a thought) Of java integration and java compatible drivers for "Cassandra"(Driver interface instance and thread alike MySQL querying and connecting driver systems)... http://cassandra.apache.org/doc/latest/getting_started/drivers.html?highlight=drivers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM