I’m trying to get into Java again after many years. Well, I’m making a servlet sample from a tutorial using Eclipse IDE and Glassfish Server 3.1.2. The sample it’s just a form sending data to another .jsp. The form is sent to the servlet, and the servlet sets a Java Bean on the output .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
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
<%@ 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. Basically it’s a string and a string[] (getters and setters included). The “DatosEncuesta” type, you see on code.
When I run this I get a NullPointerException for some reason. At first I thought it was the absence of web.xml, but I read about the annotations stuff.
Can someone help me on this one, please?
try switch the position of
jsp:useBean and jsp:getPropertyAnd also, an exception trace will help people to find out where the issue is.