JSP和Servlet NullPointer - 在文本字段中显示类属性

时间:2016-03-14 20:56:39

标签: java jsp servlets

我用JSP&创建一个项目Servlet(和实体bean),我正在尝试创建一个用户注册为客户的表单,然后重定向到预订页面。

我想保留刚刚注册的客户的ID并将其填入禁用的文本字段,然后在下一页上创建预订。但每当我尝试通过jsp加载客户类时,整个应用程序都会因NullPointerException而崩溃。

当程序到达jsp-tags以获取我的客户时,程序似乎崩溃了,因为它确实将c.cPnr打印到控制台以及JSP文件中的测试。

    <%@ page import = "g24.isp.ejb.Customer" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Welcome to Ski Village!</title>
    <link rel="stylesheet" href="new/css/normalize.css">
    <link rel="stylesheet" href="new/css/stylesheet.css">
    <link href='https://fonts.googleapis.com/css?family=Fjalla+One|Poppins:400,500,600' rel='stylesheet' type='text/css'>
    <script src="javascript/script.js" type="text/javascript"></script>
</head>
<body>
<% System.out.println("test"); %>
<% Customer c = (Customer) session.getAttribute("customer"); %>
    <div id="container">
        <!-- HEADER + MENU -->
        <header>
          <div class="logo"><!-- Ski Village Logo --></div>
          <div class="menu">
            <ul>
              <li> <a href="index.html"> Home </a></li>
              <li class="left-menu"> <a href="about.html"> About </a></li>
              <li class="right-menu"> <a href="booking.html" id="selected"> Book </a></li>
              <li> <a href="index.html" > Test</a></li>
            </ul>
          </div>
        </header>

        <!-- PAGE CONTENT -->
        <div id="wrapper">

            <div class="center-form">
                <form action="/HotelClient/HotelServlet" name="resForm" method="post">
                    <input  type="text" name="cPnr" value="<%= c.getcPnr() %>" >
                    <input  type="number" name="week" min="1" max="52" placeholder="Select week" >
                    <select name="cno">
                        <option value="1">Adventure Cabin
                        </option>
                        <option value="2">Cozy Cabin
                        </option>
                        <option value="3">Snowy Cabin
                        </option>
                        <option value="4">Hacker Cabin
                        </option>
                    </select>
                        <input type="submit" name="checkres" value="Check availability">
                        <input type="submit" name="submitresform" value="Create reservation" type="hidden">
                        <input name="operation" value="bajskorv" type="hidden">
                </form>
            </div>



        <!-- FOOTER + SOCIAL ICONS -->
            <footer>
                <a href="http://facebook.com/"><img src="img/facebook-logo.png" class="social-icon" alt="facebook logo"></a>
                <a href="http://instagram.com/"><img src="img/instagram-logo.png" class="social-icon" alt="instagram logo"></a>
                <a href="http://twitter.com/"><img src="img/twitter-logo.png" class="social-icon" alt="twitter logo"></a>
                <p>&copy; 2016 | Ski Village</p>
            </footer>

        </div>
    </div>
</body>
</html>

Servlet代码:

package g24.isp.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import g24.isp.ejb.Cabin;
import g24.isp.ejb.Customer;
import g24.isp.ejb.Hotel;
import g24.isp.ejb.Reservation;
import g24.isp.facade.Facade;
import g24.isp.facade.FacadeLocal;
import g24.isp.ejb.MethodClass;

/**
 * Servlet implementation class HotelServlet
 */
@WebServlet("/HotelServlet")
public class HotelServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @EJB
    private FacadeLocal facade;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public HotelServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        PrintWriter out = response.getWriter();
        out.println("MainServlet-doGet");

        out.close();
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub

        String url = "did not get an url";
        // Get hidden field
        String operation = request.getParameter("operation");

        MethodClass mc = new MethodClass();


        if (operation.equals("createcustomer")) {
            String cPnr = request.getParameter("txtcPnr");
            String cAddress = request.getParameter("txtcAddress");
            String cPhone = request.getParameter("txtcPhone");
            String cName = request.getParameter("txtcName");

            if (facade.findByCpnr(cPnr) == null) {
                Customer customer = new Customer();
                customer.setcPnr(cPnr);
                customer.setcAddress(cAddress);
                customer.setcPhone(cPhone);
                customer.setcName(cName);
                facade.createCustomer(customer);
                url = "/new/reservation.jsp";
            } else {
                url = "new/newcust.jsp";
            }
        }

        else if (operation.equals("createreservation")) {
            String cpnr = request.getParameter("txtcPnr");
            int week = mc.ParseStringToInt(request.getParameter("week"));
            int cno = mc.ParseStringToInt(request.getParameter("cno"));

            Customer cs = facade.findByCpnr(cpnr);
            Cabin cb = facade.findByCabinNo(cno);

            if (cb != null && cs != null) {
                Reservation res = new Reservation();
                res.setCabin(cb);
                res.setCustomer(cs);
                res.setrDate(week);

                facade.createReservation(res);
                url = "/Index.jsp";
            } else {
                System.out.println("Did not enter if statement");
                url = "/Index.jsp";
            }

        } 

        else if (operation.equals("newcustomer")) {
            url = "/new/newcust.jsp";
        }

        else if (operation.equals("setcustomer")) {
            System.out.println("Servlet - Create reservation");

            String cpnr = request.getParameter("txtcPnr");
            System.out.println(cpnr);
            url = "/new/reservation.jsp";

             Customer customer = facade.findByCpnr(cpnr);
             if (customer != null) {
             System.out.println(customer.getcName());
             session.setAttribute("customer", customer);
             url = "/reservation.jsp";
             }

             else {
             System.out.println("Customer value is null");
             }
        }


        System.out.println(url);
        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
        dispatcher.forward(request, response);
    }
}

2 个答案:

答案 0 :(得分:0)

可能是它没有设置会话属性,当c.getcPnr()执行时,它会抛出NullPointerException。你能检查一下属性是否在session中设置了吗?您可以尝试在jsp中打印出'c'。

答案 1 :(得分:0)

我解决了这个问题 - 我试图通过访问facade.findBycPnr()方法找到客户。

解决方案是在创建客户时使用session.setAttribute(customer,cust)。