Olá a todos. Pela pergunta, vocês já devem ter percebido que sou novato na area de desenvolvimento Web.
Criei um projetinho simples de cadastro de notícias, só para brincar um pouco. Tenho uma página JSP na qual o usuário cadastra o autor e a notícia. A idéia era ter um Servlet que receba a requisição do cadastro da notícia, valide e responda para a mesma página, em alguns campos mais abaixo de onde foi preenchido os dados. O problema é que não estou conseguindo fazer com que o servlet responda a mesma página da requisição.
Código do Servlet:
package br.com.Servlets;
import br.com.JavaBeans.Noticia;
import java.io.PrintWriter;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
public class ServletCadNews extends HttpServlet{
@Override
public void init() throws ServletException{
super.init();
}
@Override
public void destroy(){
super.destroy();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
resp.setContentType("text/html; charset=ISO-8859-1");
String autor = req.getParameter("autor");
String noticia = req.getParameter("noticia");
Noticia n = new Noticia();
n.setAutor(autor);
n.setNoticia(noticia);
//Aqui que não sei o que colocar para que este servlet responda à página...
resp.sendRedirect("Home.jsp");
}
}
... e o código da página JSP:
<%@page contentType="text/html"
pageEncoding="UTF-8"
import="br.com.JavaBeans.Noticia"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean id="noticia" class="br.com.JavaBeans.Noticia" />
<jsp:setProperty name="noticia" property="*" />
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Notícias - Página Inicial</title>
</head>
<body>
<div id="cabecalho" style="position:absolute; width:800px; top:20px; background-color:green">-</div>
<h3 style="position:absolute; left:830px; top:1px">Cadastro de notícias</h3>
<form name="formCadastroNews" action="ServletCadNews" method="POST">
<h4 style="position:absolute; top:35px; left:20px">Autor: </h4> <input type="text" name="autor" value="" style="position:absolute; top:55px; left:100px; width:180px" />
<h4 style="position:absolute; top:75px; left:20px">Notícia</h4> <textarea name="noticia" rows="4" cols="20" style="position: absolute; left:100px; top:95px"></textarea>
<input type="submit" value="Cadastrar" name="btCadastroNew" style="position:absolute; top:200px"/>
<input type="reset" value="Limpar" name="btLimpar" style="position:absolute; top:200px; left:100px; width:80px"/>
</form>
<div id="cabecalho" style="position:absolute; width:800px; top:270px; background-color:green">-</div>
<h3 style="position:absolute; left:830px; top:251px">Notícias Cadastradas</h3>
<h4 style="font-size:18px ;position:absolute; top:290px; left:20px">Autor:</h4><%= noticia.getAutor() %>
</body>
</html>
Obrigado pela atenção dos colegas.
Boa tarde.
