可能重复:
Calling servlet from HTML form, but servlet is never invoked
我从html表单调用servlet,servlet接受表单数据,它会将表单数据插入到数据库中。但是当我点击提交按钮错误页面即将到来。请帮助我的servlet代码中有什么错误。
我的HTML代码:
<html>
<head>
<title> Sign Up </title>
</head>
<body>
<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>
</body>
</html>
我的servlet代码:
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);
}
}
}
}
我的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-app>
答案 0 :(得分:1)
我认为,错误就在这里:<form action="servlet/Loginservlet"
试试这个<form action="/<your-app-name>/login"
答案 1 :(得分:0)
因为在表单操作中您已经提供了servlet/Loginservlet
。您应该在web.xml
中将地图映射为
<url-pattern>/servlet/Loginservlet</url-pattern>
或更改
之类的表格action='login'
你可以做以上任何一种。
答案 2 :(得分:0)
试试这个
<form action="login" method="post" >
你的servlet网址是
<url-pattern>/login</url-pattern>
当你为本地主机运行你的web应用程序时,那么网址就像
localhost:8080/test
然后测试是您的应用程序名称的名称,其中包含您的Web应用程序到Web目录。现在假设你创建了索引文件,那么它将只从这个和另一个页面URL(如
)运行索引文件localhost:8080/test/page1.html
现在从page1.html开始登录页面,然后链接看起来像是servlet
localhost:8080/test/login
定义特定servlet的url模式,你必须设置url以调用上面的特定servlet。此URL将隐藏客户端的实际servlet,您可以在url模式中设置任何内容