[Resolvido]Como adicionar e redirecionar páginas com Servlets?

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 = &lt;input type="text" name="usuario" /&gt;<br />
                Senha = &lt;input type="password" name="senha" /&gt;<br />
                &lt;input type="submit" value="logar" /&gt;
            &lt;/form&gt;
        &lt;/body&gt;
    &lt;/html&gt;

Aqui é a outra página que quero redirecionar pra ela:

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd"&gt;

&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;JSP Page&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;form action = "NovoServlet?usuario=a&senha=a" method="get"&gt;
            &lt;h1&gt;Teste&lt;/h1&gt;
        &lt;/form&gt;
    &lt;/body&gt;
&lt;/html&gt;

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(&quot;text/html;charset=ISO-8859-1&quot;);
        
        String usuario = request.getParameter(&quot;usuario&quot;);
        String senha = request.getParameter(&quot;senha&quot;);
        
        if(usuario.equals(&quot;a&quot;) && senha.equals(&quot;a&quot;)){
            response.sendRedirect(&quot;teste.jsp&quot;);
        }
          
    }
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
         
    }
    public void init() throws ServletException {
        super.init();
    }
    public String getServletInfo() {
        return &quot;Short description&quot;;
    }
}

E finalmente meu arquivo.xml:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;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"&gt;
    &lt;servlet&gt;
        &lt;servlet-name&gt;NovoServlet&lt;/servlet-name&gt;
        &lt;jsp-file&gt;/index.jsp&lt;/jsp-file&gt;
        &lt;/servlet&gt;
    &lt;servlet-mapping&gt;
        &lt;servlet-name&gt;NovoServlet&lt;/servlet-name&gt;
        &lt;url-pattern&gt;/NovoServlet&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
    &lt;session-config&gt;
        &lt;session-timeout&gt;
            30
        &lt;/session-timeout&gt;
    &lt;/session-config&gt;
    &lt;welcome-file-list&gt;
        &lt;welcome-file&gt;teste.jsp&lt;/welcome-file&gt;
        &lt;/welcome-file-list&gt;
    &lt;/web-app&gt;

RequestDispatcher r = request.getRequestDispatcher( "teste.jsp" ); r.forward( request, response );

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)…

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>

[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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;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"&gt;
    &lt;servlet&gt;
        &lt;servlet-name&gt;NovoServlet&lt;/servlet-name&gt;
        &lt;servlet-class&gt;teste.NovoServlet&lt;/servlet-class&gt;
    &lt;/servlet&gt;
    &lt;servlet-mapping&gt;
        &lt;servlet-name&gt;NovoServlet&lt;/servlet-name&gt;
        &lt;url-pattern&gt;/NovoServlet&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
    &lt;session-config&gt;
        &lt;session-timeout&gt;
            30
        &lt;/session-timeout&gt;
    &lt;/session-config&gt;
    &lt;welcome-file-list&gt;
        &lt;welcome-file&gt;welcome.html&lt;/welcome-file&gt;
        &lt;/welcome-file-list&gt;
&lt;/web-app&gt;

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)

&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;&lt;/title&gt;
  &lt;/head&gt;
  &lt;form action="NovoServlet" method="post"&gt;
      &lt;body&gt;
          &lt;h1&gt;bem vindo!!&lt;/h1&gt;
          nome: &lt;input type="text" name="nome"/&gt;<br />
          &lt;input type="submit" name="ok" value="ok"/&gt;
      &lt;/body&gt;
  &lt;/form&gt;
&lt;/html&gt;

E a página para a qual queria redirecionar (html)

&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;h1&gt;Funcionou!!!!!&lt;/h1&gt;
  &lt;/body&gt;
&lt;/html&gt;

Agora tá funcionando igual ao que eu queria, obrigado a todos que postaram me ajudando!!!

Acredito que tenha sido o que eu falei, você colocou a barra na action…
=D