Erro ao rodar um servlet

2 respostas
D

Saudações.
Estou executando um servlet no Tomcat.
O servlet chamado Login tem o código:

//package classes;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//import classes.*;

public class Login extends HttpServlet {

//  Tempo de sessão: 10 minutos
    final private static int tempoDeSessao = 10 * 60;
    
    protected void doGet(
            final HttpServletRequest req,
            final HttpServletResponse res)
            throws ServletException, IOException{
        res.setContentType("text/html");
        res.getWriter().println("<html><header><meta HTTP-EQUIV="Pragma" " +
                                "CONTENT="no-cache"></header>" +
                                "<title> Ead ChAt - Ensino á Distância Unerj </title><body>");
        res.getWriter().println("Login para o chat: " +
                                  "<form action="" +
                                  this.getClass().getName() +
        "" method=post><input name=login><input type=submit" + 
        " value=Login></form></html>");
    }
    /* O método POST checa a viabilidade de criar um usuário
     * e o cria */
    	protected void doPost(final HttpServletRequest req,
    	        final HttpServletResponse res)
    	throws ServletException, IOException{
//    	  Seta o tipo de conteúdo
            res.setContentType("text/html");
        // Se já existe uma sessão, invalida ela
            if (req.getSession() != null 
                && req.getSession().getAttribute("user") != null) {
                    req.getSession().invalidate();
            }
            try{
                // Cria um usuário
                Usuario usuario = new UsuarioChat(req.getParameter("login"));
                req.getSession().setAttribute("user", usuario);
                
                // Tempo Limite
                req.getSession().setMaxInactiveInterval(tempoDeSessao);
            }catch(IllegalArgumentException iaex){
                // se já existe um usuário, mostra pagina de login novamente
    			res.getWriter().println("<html><header><meta HTTP-EQUIV="Pragma" CONTENT="no-cache"></header><body>");
    			res.getWriter().println(
    					""
    						+ "
	Ja existe um usuario com esse login:<br>"
    						+ "
	<form action="
    						+ this.getClass().getName()
    						+ " method=post>"
    						+ "
		<input name=login>"
    						+ "
		<input type=submit value=Login>"
    						+ "
	</form>"
    						+ "
</html>");
                return;
             }
            
        	// Mostra os dois frames
            res.getWriter().print(
                	"<html>
"
    				+ "	<frameset rows=*,100>
"
    				+ "		<frame name="text" src=""
    				+ this.getClass().getPackage().getName()
    				+ ".Sala">
"
    				+ "		<frame name="form" src=""
    				+ this.getClass().getPackage().getName()
    				+ ".Form">
"
    				+ "	</frameset>
"
    				+ "</html>");
    		return;
       
    	}
}

Ele é executado normalmente, mas quando digito o login na caixa de texto e mando e clico em Login aparece:

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

java.lang.NullPointerException
	Login.doPost(Login.java:68)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:763)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:856)

note The full stack trace of the root cause is available in the Tomcat logs.
Apache Tomcat/5.0.19

Alguém sabe como corrigir?

2 Respostas

D

Gente consegui resolver aquele problema, mas agora surgiu outro, vejam o que aparece agora:

HTTP Status 405 - HTTP method POST is not supported by this URL

type Status report

message HTTP method POST is not supported by this URL

description The specified HTTP method is not allowed for the requested resource (HTTP method POST is not supported by this URL).
Apache Tomcat/5.0.19
pcalcado

Dê uma olhada na assinatura do método doPost.

Ah, pergunta: por que usar HTML no servlet?
[]s

Criado 14 de maio de 2004
Ultima resposta 14 de mai. de 2004
Respostas 2
Participantes 2