package Ejer2;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.*;
@SuppressWarnings("deprecation")
@WebServlet(urlPatterns="/Registro")
public class Registro extends HttpServlet implements SingleThreadModel{
private static final long serialVersionUID = 1L;
public Registro() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
HttpSession session=req.getSession(true);
Usuario miuser=(Usuario)session.getValue(session.getId());
if(miuser==null){
miuser=new Usuario(req.getParameter("user"),req.getParameter("password"));
session.putValue(session.getId(),miuser);
}
res.setContentType("text/html");
String user=req.getParameter("user");
//String pass = req.getParameter("pass");
PrintWriter toClient = res.getWriter();
toClient.println("<html>");
toClient.println("<title>REGISTRO REALIZADO</title>");
toClient.println("Usuario "+user+" registrado con exito");
toClient.println("</html>");
toClient.close();
}
}
registro.html是:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Registro</title>
</head>
<body>
<form action="/Ejer2/Registro" method="POST">
<input type=hidden name=registro value=resultadoRegistro>
<BR><BR>Username: <input type=text name=user>
<BR><BR>Password: <input type=password name=pass>
<BR><BR><input type=submit value="Enviar"><input type=reset>
</form>
</body>
</html>
当我运行registro.html时,一切都按预期进行:
但是,当我输入用户名和密码时,它不起作用:
4
似乎没有找到Registro.java。我尝试将
action="/Ejer2/Registro"
更改为许多其他内容,例如/Registro
或完整的http://...
,但仍然无法正常工作。这是我的web.xml:
可能是什么问题?
最佳答案
我猜您在web.xml中缺少servlet映射。您需要在web.xml中注册servlet(打开web.xml文件,并在底部的“更改”选项卡中查看实际的源代码而非设计器),添加以下代码,您应该会很高兴
<servlet>
<servlet-name>RegistroServlet</servlet-name>
<servlet-class>Ejer2.Registro</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegistroServlet</servlet-name>
<url-pattern>/Registro</url-pattern>
</servlet-mapping>
我还建议您在尝试编写Web应用程序之前先退一步并从基本的Java开始。您的代码有几个新手问题:
1)软件包名称应以小写字母开头!
2)网址映射也应使用小写字母,例如/ registro
在表单操作中更改url以匹配urlmapping。在你的情况下
表单action =“ / Registro” ...
Ejer2是软件包的名称,它与url映射无关。希望它能帮助您解决问题