gpt4 book ai didi

java - Servlet 不设置属性?

转载 作者:行者123 更新时间:2023-12-02 06:06:49 25 4
gpt4 key购买 nike

我刚刚开始使用连接池,似乎无法完成任何工作,例如获取数据库中的所有模块。

这是我的 testconnectionservlet,这一定是问题所在:

 package Servlet;


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

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 DTO.Module;
import DAO.*;
import Service.ModuleService;



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

/**
* @see HttpServlet#HttpServlet()
*/
public TestConnectionServlet()
{
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
processRequest(request, response);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
processRequest(request, response);
}

private void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{

String forwardToJsp = "";
HttpSession session = request.getSession();
String clientSessionId = session.getId();

//Check the 'action' parameter to see what the user wants...
if ( request.getParameter("action") != null)
{
// Create a LoginCommand and execute it




ModuleService mService = new ModuleService();
if (request.getParameter("action") == "allModules") {
List allModules = mService.getAllModules();
session.setAttribute("theModulesNoOrder", allModules);
forwardToJsp = "all-Modules.jsp";
} else if (request.getParameter("action") == "allModulesAsc") {
List allModulesAsc = mService.getAllModulesCodeAsc();
session.setAttribute("theModulesAsc", allModulesAsc);
forwardToJsp = "whatever.jsp";
}
}

//Get the request dispatcher object and forward the request to the appropriate JSP page...
//RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(forwardToJsp);
//dispatcher.forward(request, response);

//to stop UserActionServelet coming up in URL when logging in.
//lines above were in it before
response.sendRedirect(forwardToJsp);
}
}

有人注意到有什么异常吗?

当我在 JSP 中执行此类操作时,我收到 java.lang.NullPointerException,但是 servlet 中不应该出现这种情况吗?:

    <%
List<Module> modules;
modules = (List)(request.getSession().getAttribute("theModulesAsc"));
out.print(modules.size());
%>

堆栈跟踪:

org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
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.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

最佳答案

改变

if (request.getParameter("action") == "allModules") {

使用equals()

关于java - Servlet 不设置属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22208008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com