Bom pessoal, mais uma vez preciso de ajuda de quem entende!!
Como eu faço pra adicionar mais um página e redirecionar essa página? Aqui eu tenho um campo pra digitar nome e senha ai quando o usuário digitar os dados corretos, eu quero redirecionar para uma outra página, só que não to conseguindo colocá-la no arquivo.xml de configuração, ela fica como página de welcome-file
Esse é o código da página onde tem os campos…:
<html>
<head>
<title>Login e Senha</title>
</head>
<body>
<form action = "/NovoServlet" method="get">
Login = <input type="text" name="usuario" /><br />
Senha = <input type="password" name="senha" /><br />
<input type="submit" value="logar" />
</form>
</body>
</html>
Aqui é a outra página que quero redirecionar pra ela:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<form action = "NovoServlet?usuario=a&senha=a" method="get">
<h1>Teste</h1>
</form>
</body>
</html>
E esse é meu arquivo .jsp estou usando o método get :
public class NovoServlet extends HttpServlet {
public void destroy(){
super.destroy();
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=ISO-8859-1");
String usuario = request.getParameter("usuario");
String senha = request.getParameter("senha");
if(usuario.equals("a") && senha.equals("a")){
response.sendRedirect("teste.jsp");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
public void init() throws ServletException {
super.init();
}
public String getServletInfo() {
return "Short description";
}
}
E finalmente meu arquivo.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>NovoServlet</servlet-name>
<jsp-file>/index.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>NovoServlet</servlet-name>
<url-pattern>/NovoServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>teste.jsp</welcome-file>
</welcome-file-list>
</web-app>
RequestDispatcher r = request.getRequestDispatcher( "teste.jsp" );
r.forward( request, response );
volnei
Janeiro 15, 2008, 9:51am
#4
moacirjava:
Não funcionou
Como assim??? oque aconteceu???
dá um erro no browser, e aparece escrito:
Precisa falar mais alguma coisa? Vc está redirecionando para um caminho que não existe (/NovoServlet)…
Baldao
Janeiro 15, 2008, 11:45am
#7
Perae… tem algo bem errado aí… vc colocou um servlet dentro de uma página JSP???
O Servlet é um arquivo .java… seu erro tá aí
Tá usando Eclipse ou NetBeans? Cria dentro da pasta src uma classe NovoServlet com o conteúdo que vc colocou em /index.jsp dae vc acerta o mapeamento:
<servlet>
<servlet-name>NovoServlet</servlet-name>
<jsp-file>/index.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>NovoServlet</servlet-name>
<url-pattern>/NovoServlet</url-pattern>
</servlet-mapping>
Se não estiver usando IDE vai ter que compilar o servlet e colocar a classe na pasta /WEB-INF/classes
Precisa falar mais alguma coisa? Vc está redirecionando para um caminho que não existe (/NovoServlet)…[/quote]
Mas que caminho é esse???
.java
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=ISO-8859-1");
String usuario = request.getParameter("usuario");
String senha = request.getParameter("senha");
if((usuario.equals("a")) && (senha.equals("a"))){
//response.sendRedirect("/NovoServlet/teste.jsp");
RequestDispatcher r = request.getRequestDispatcher("/NovoSerlvet/teste.jsp");
r.forward( request, response );
}
}
.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>NovoServlet</servlet-name>
<jsp-file>/teste.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>NovoServlet</servlet-name>
<url-pattern>/NovoServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<jsp-config>
</jsp-config>
</web-app>
index.jsp
<html>
<head>
<title>Login e Senha</title>
</head>
<body>
<form action = "/NovoServlet" method="get">
Login = <input type="text" name="usuario" /><br />
Senha = <input type="password" name="senha" /><br />
<input type="submit" value="logar" />
</form>
</body>
</html>
teste.jsp
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<form action = "/NovoServlet/teste.jsp" method="get">
<h1>Teste</h1>
</form>
</body>
</html>
LuizLG
Janeiro 15, 2008, 12:17pm
#9
[code]
Login e Senha
Login =
Senha =
[/code]
Não seria sem a barra “/” ?
<form action = "NovoServlet" method="get">
Galera, resolvi apagar aquele projeto e fazer esse, deu certo, mas não me perguntem o problema do antigo… Todos foram feitos no netbeans.
Esse é o .xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>NovoServlet</servlet-name>
<servlet-class>teste.NovoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>NovoServlet</servlet-name>
<url-pattern>/NovoServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>welcome.html</welcome-file>
</welcome-file-list>
</web-app>
Essa é minha servlet
package teste;
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class NovoServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//processRequest(request, response);
String teste = request.getParameter("nome");
if(teste.equals("a")){
response.sendRedirect("funcionou.html");
}
}
}
Minha página principal (html)
<html>
<head>
<title></title>
</head>
<form action="NovoServlet" method="post">
<body>
<h1>bem vindo!!</h1>
nome: <input type="text" name="nome"/><br />
<input type="submit" name="ok" value="ok"/>
</body>
</form>
</html>
E a página para a qual queria redirecionar (html)
<html>
<head>
<title></title>
</head>
<body>
<h1>Funcionou!!!!!</h1>
</body>
</html>
Agora tá funcionando igual ao que eu queria, obrigado a todos que postaram me ajudando!!!
LuizLG
Janeiro 16, 2008, 12:44pm
#11
Acredito que tenha sido o que eu falei, você colocou a barra na action…
=D