Pessoal,
saudações!
Estou desenvolvendo um projeto, mas estou com um problema onde a minha Servlet não esta enviando informações para JSP, basicamente estou utilizando setAttribute e requestDispatcher na servlet para enviar informações para JSP mas não envia nenhuma informação para JSP.
Ja tentei tambem session , getServletContext() e respectivamente na JSP getAttribute(), coloquei javabenas/jsp:setProperty, mas não funcionou…
Por favor alguem pode-me ajudar?
segue os codigos:
Servlet:
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package br.com.noticiasWeb.controler;
//import br.com.noticiasWeb.controler.consistencias.Classe_consiste_noticia;
import br.com.noticiasWeb.modelo.dao.NoticiaDAO;
import br.com.noticiasWeb.modelo.javaBeans.Noticia;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
//import javax.faces.context.FacesContext;
//import java.util.*;
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;
/**
*
* @author Wanderlei
*/
@WebServlet(name = "ServletController", urlPatterns = {"/ServletController"})
public class ServletControler extends HttpServlet {
//private static final long serialVersionUID =
/**
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//super.service(request, response);
String logica = request.getParameter("Logica");
System.out.println("==> logica= " + logica);
if (logica.equals("cadastrar_Noticia")) {
salvar_Noticia(request, response);
} else {
if (logica.equals("ver_Noticia")) {
try {
visualizar_Noticia(request, response);
} catch (ParseException ex) {
Logger.getLogger(ServletControler.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
private void visualizar_Noticia(HttpServletRequest request, HttpServletResponse response) throws ParseException, ServletException, IOException {
// Recuperando noticia
NoticiaDAO nDAO = new NoticiaDAO();
/* System.out.println("1$ id_usuario: " + request.getParameter("id_usuario"));
// System.out.println(" dt noticia: " + request.getParameter("data_noticia_DMA"));
System.out.println("2 dt noticia AMD: " + request.getParameter("data_noticia_AMD"));
System.out.println("3 hora : " + request.getParameter("hora_noticia"));
System.out.println("4 micro segundo: " + request.getParameter("micro_segundo"));
*/
Noticia noticia = new Noticia();
String w_dat_str = request.getParameter("data_noticia_AMD");
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
java.sql.Date w_dat_time =
new java.sql.Date(format1.parse(request.getParameter("data_noticia_AMD")).getTime());
Time w1_hora_noticia = Time.valueOf(request.getParameter("hora_noticia"));
/* System.out.println("5 data_noticia: " + w_dat_time);
System.out.println("6 usuario: " + Integer.parseInt(request.getParameter("id_usuario")));
System.out.println("7 micro_segundo: " + Integer.parseInt(request.getParameter("micro_segundo")));
*/
try {
Noticia n = nDAO.consulta_noticia((Integer.parseInt(request.getParameter("id_usuario"))), w_dat_time, w1_hora_noticia, (Integer.parseInt(request.getParameter("micro_segundo"))));
} catch (SQLException ex) {
Logger.getLogger(ServletControler.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("#8 visualizar_Noticia");
// redireciona parapagina de consulta materia
/// noticia.setHora_noticia_str(request.getParameter("hora_noticia"));
// noticia.setHora_noticia(null);
request.setAttribute("noticia", noticia);
// getServletContext().setAttribute("noticia", noticia);
//this.getServletContext().setAttribute("noticia",noticia);
//response.sendRedirect("/noticia.jsp");
//FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("noticia",noticia);
RequestDispatcher rd = request.getRequestDispatcher("/noticia.jsp");
rd.forward(request, response);
// getServletConfig().getServletContext().getRequestDispatcher("/noticia.jsp").forward(request, response);
}
JSP:
<%@page import="javax.faces.context.FacesContext"%>
<%@page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%-- <!DOCTYPE html> --%>
<c:import url="cabecalho_noticias.jsp"/>
<%--
<jsp:useBean id="noticia" class="br.com.noticiasWeb.modelo.javaBeans.Noticia" scope="request">
</jsp:useBean>
<c:out value='${noticia}'/>
<%--
<jsp:setProperty name="noticia" property="*" />
--%>
<%--
<%
Object noticia = request.getAttribute("noticia");
%>
--%>
<%--
<% request.getSession().getAttribute("noticia"); %>
<%--
<%
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("noticia");
%>
<% noticia.getMember().setNome(request.getParameter("data_noticia_AMD")); %>
<%--
Object noticia = request.getAttribute("noticia");
%>
<%--
<jsp:getProperty name="noticia" property="data_noticia_AMD" />
--%>
<%--
<jsp:setProperty name="noticia" property="materia" value="${materia}" ></jsp:setProperty>
<jsp:setProperty name="noticia" property="id_usuario" value="${id_usuario}" ></jsp:setProperty>
<jsp:setProperty name="noticia" property="titulo_manchete" value="${titulo_manchete}" ></jsp:setProperty>
<jsp:setProperty name="noticia" property="hora_noticia" value="${hora_noticia}" ></jsp:setProperty>
<%--
<jsp:setProperty name="noticia" property="data_noticia_AMD" value="${data_noticia_AMD}" ></jsp:setProperty>
--%>
<table cellpadding="4">
<tr>
<td><b>Data:</b></td>
<td><fmt:formatDate value="${noticia.data_noticia_AMD.time}" pattern="dd/MM/yyyy"/></td>
</tr>
<tr>
<td><b>Hora:</b></td>
<td>${noticia.hora_noticia}</td>
</tr>
<tr>
<td><b>Autor:</b></td>
<td>${noticia.id_usuario}</td>
</tr>
<tr>
<td><b>Manchete:</b></td>
<td>${noticia.titulo_manchete}</td>
</tr>
<tr>
<td><b>Noticia:</b></td>
<td width="1000px">${noticia.materia}</td>
</tr>
</table>
<c:import url="rodape_noticias.jsp"/>