Java 从HTML表单调用servlet,但从未调用servlet
我从html表单调用servlet,servlet获取表单数据,并将表单数据插入数据库。但当我单击“提交”按钮时,错误页面即将出现。请帮助我的servlet代码中的错误 我的servlet代码:Java 从HTML表单调用servlet,但从未调用servlet,java,html,Java,Html,我从html表单调用servlet,servlet获取表单数据,并将表单数据插入数据库。但当我单击“提交”按钮时,错误页面即将出现。请帮助我的servlet代码中的错误 我的servlet代码: import javax.servlet.http.HttpServlet; import java.io.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; public class Login
import javax.servlet.http.HttpServlet;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Loginservlet extends HttpServlet {
public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
System.out.println("login servlet");
String connectionURL = "jdbc:mysql://localhost:3306/mysql";
Connection connection=null;
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String username= req.getParameter("username");
String password = req.getParameter("password");
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(connectionURL, "root", "root");
String sql = "insert into signup values (?,?)";
PreparedStatement pst = connection.prepareStatement(sql);
pst.setString(1, username);
pst.setString(2, password);
int numRowsChanged = pst.executeUpdate();
out.println(" Data has been submitted ");
pst.close();
}
catch(ClassNotFoundException e){
out.println("Couldn't load database driver: "+ e.getMessage());
}
catch(SQLException e){
out.println("SQLException caught: " + e.getMessage());
}
catch (Exception e){
out.println(e);
}
finally {
try {
if (connection != null)
connection.close();
}
catch (SQLException ignored){
out.println(ignored);
}
}
}
}
我的html代码:
报名
<form action="servlet/Loginservlet" method="post" >
<font size='5'>Create your Account:</font><br/><br>
<label for="username" accesskey="u" style="padding-left:3px;">User Name: </label>
<input type="text" style="background-color:#ffffff;margin-left:14px;padding-top:7px;border-width:0px;margin-top:6px;padding-right:85px;" id="username" name="username" tabindex="1"><br/><br>
<label for="password" accesskey="p" style="padding-left:4px;">Password: </label>
<input type="password" style="background-color:#ffffff;margin-left:14px;padding-top:7px;border-width:0px;padding-right:85px;" id="password" name="pasword" tabindex="2"><br/><br>
<input type="submit" value="Submit" style="margin-left:164px;"/>
<input type="reset" value="Reset" style="margin-left:17px;"/>
</form>
创建您的帐户:
用户名:
密码:
web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>Loginservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
登录
罗金瑟夫莱特酒店
登录
/登录
请帮助检查项目的
web.xml
文件,您必须在那里注册servlet。也
在html中使用
及
在web.xml中
<servlet-class>your.class.package.Loginservlet</servlet-class>
</servlet>
your.class.package.Loginservlet
在查看servlet类时,没有定义必需的包。并在
标记中将该类与包(表示完全限定名)映射
另一件事是您正在将action设置为urlservlet/LogininServlet
,但在
标记中给定了不同的url,这是错误的。您可以简单地将表单操作设置为login一切正常…在html页面中使用action=“/login”…它会起作用。我也做了同样的操作您还应该发布您使用的web.xml
文件的摘录哪个错误发生了?控制台中是否也存在任何异常?正如fyr所指出的,您必须在URL处注册servlet,以便服务器使用它。如果您已经这样做了,请检查控制台/日志,查看在加载servlet类/实例化它时是否存在异常。@fyr:现在我发布了web.xml文件是的,我在web.xml文件中注册了servlet我已经编辑了答案我正在用Myeclipse IDE进行编程您是否收到404或任何其他错误页面?这不是404错误页面……是的“此程序无法显示网页。”斜杠前的点应该可以做到这一点