[英]Java - NullPointerException with JSP/Servlet sample?
I'm trying to get into Java again after many years. 多年后,我试图再次进入Java。 Well, I'm making a servlet sample from a tutorial using Eclipse IDE and Glassfish Server 3.1.2.
好吧,我正在使用Eclipse IDE和Glassfish Server 3.1.2从一个教程中制作一个servlet示例。 The sample it's just a form sending data to another .jsp.
该示例只是将数据发送到另一个.jsp的一种形式。 The form is sent to the servlet, and the servlet sets a Java Bean on the output .jsp.
表单被发送到servlet,并且servlet在输出.jsp上设置Java Bean。
index.jsp index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Encuesta de Desarrolladores</title>
</head>
<body>
<h1>Bienvenido a la encuesta de desarrolladores!</h1>
<p>Indica los lenguajes de programación con los que estas familiarizado</p>
<form action="ServletController" method="post">
<table>
<tr>
<td>Nombre Completo:</td>
<td><input type="text" name="nombreCompleto" value=""/></td>
</tr>
<tr>
<td>Java:</td>
<td><input type="checkbox" name="progLeng" value="java"/></td>
</tr>
<tr>
<td>PHP:</td>
<td><input type="checkbox" name="progLeng" value="php"/></td>
</tr>
<tr>
<td>Python:</td>
<td><input type="checkbox" name="progLeng" value="python"/></td>
</tr>
<tr>
<td>Ruby:</td>
<td><input type="checkbox" name="progLeng" value="ruby"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Enviar"/></td>
</tr>
</table>
</form>
</body>
</html>
ServletController.java ServletController.java
package com.j2ee.servlet;
import java.io.IOException;
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 com.j2ee.bean.DatosEncuesta;;
@WebServlet(name="ServletController", urlPatterns ={"/ServletController"})
public class ServletController extends HttpServlet {
private static final long serialVersionUID = 1L;
public ServletController() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
func(request, response);
}
protected void func(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
DatosEncuesta datosEncuesta = new DatosEncuesta();
datosEncuesta.setNombreCompleto(req.getParameter("nombreCompleto"));
datosEncuesta.setProgLeng(req.getParameterValues("progLeng"));
req.setAttribute("datosEncuesta", datosEncuesta);
req.getRequestDispatcher("salida.jsp").forward(req, res);
}
}
salida.jsp salida.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Gracias!</title>
</head>
<body>
<h2>Gracias por cubrir nuestra encuesta!</h2>
<p>
<jsp:getProperty name="DatosEncuesta" property="nombreCompleto" />
Nos has indicado que estas familiarizado con los siguientes lenguajes de programación:
<jsp:useBean id="DatosEncuesta" scope="request" class="com.j2ee.bean.DatosEncuesta" />
</p>
<ul>
<%
System.out.println("Llegue a JSP!");
String[] lenguajesSeleccionados = DatosEncuesta.getProgLeng();
if(lenguajesSeleccionados != null)
{
for(int i=0; i<lenguajesSeleccionados.length; i++){
%>
<li>
<%=lenguajesSeleccionados[i] %>
</li>
<% }
}%>
</ul>
</body>
</html>
I would put the java bean, but it's kind of obvious. 我将放置Java bean,但这是显而易见的。 Basically it's a string and a string[] (getters and setters included).
基本上,它是一个字符串和一个string [](包括getter和setter)。 The "DatosEncuesta" type, you see on code.
您将在代码上看到“ DatosEncuesta”类型。
When I run this I get a NullPointerException for some reason. 当我运行它时,由于某种原因,我得到了NullPointerException。 At first I thought it was the absence of web.xml, but I read about the annotations stuff.
起初我以为缺少web.xml,但是我读到了有关注释的内容。
Can someone help me on this one, please? 有人可以帮我吗?
尝试切换jsp:useBean and jsp:getProperty
而且,异常跟踪将帮助人们找出问题所在。
jsp:useBean and jsp:getProperty is not correct way for getting values from request object. jsp:useBean和jsp:getProperty不是从请求对象获取值的正确方法。 just replace your salida.jsp with below code.
只需将您的salida.jsp替换为以下代码即可。 It will print selected checkbox from index.jsp.
它将从index.jsp打印选定的复选框。 Let me know if there is still problem.
让我知道是否还有问题。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="com.j2ee.bean.DatosEncuesta;"%>
<!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=ISO-8859-1">
<title>Gracias!</title>
</head>
<body>
<h2>Gracias por cubrir nuestra encuesta!</h2>
<p>
<ul>
<%
DatosEncuesta objects = (DatosEncuesta) request.getAttribute("datosEncuesta");
%>
<%
System.out.println("Llegue a JSP!");
String[] lenguajesSeleccionados = objects.getProgLeng();
if (lenguajesSeleccionados != null) {
for (int i = 0; i < lenguajesSeleccionados.length; i++) {
%>
<li><%=lenguajesSeleccionados[i]%></li>
<%
}
}
%>
</ul>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.