在Servlet Post方法中没有从html页面调用



我对如何在java中调用post方法有问题。当我按下登录页面上的点击按钮后,浏览器上没有显示任何东西。请告诉我正确的解决方案。我的输出显示在控制台上。所以请告诉我什么是错误的。提前谢谢。

login.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<link rel="stylesheet" type="text/css" href="css/loginPage.css" />
<!-- <script src="js/jquery-ui.1.10.4.js"></script> -->
<title>Login Page</title>
</head>
<section class="login">
<div class="titulo">Staff Login</div>
<form action="/AdminLogin" name="AdminLogin" method="post">
<input type="text" title="Username required" placeholder="Username" data-icon="U">
<input type="password" title="Password required" placeholder="Password" data-icon="x">
<div class="olvido">
<div class="col"><a href="#" title="Ver Carásteres">Register Student</a></div>
<div class="col"><a href="#" title="Recuperar Password">Fotgot Password?</a></div>
</div>
<button class="enviar">submit</button>
</form>
</section>
</html>

AdminLogin.java

package com.admin.main;
import java.beans.Statement;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletConfig;
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 com.admin.dao.UserMasterConnection;
@WebServlet("/AdminLogin")
public class AdminLogin extends HttpServlet {
private static final long serialVersionUID = 1L;
Statement stmt;
UserMasterConnection userMasterConnection = new UserMasterConnection();
public AdminLogin()
{
super();
}
public void init(ServletConfig config) throws ServletException {

}
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("doget");
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException 
{
System.out.println("doPost()");
/*System.out.println("dopost");
// connection with database
// Connection connection =
Connection connection = userMasterConnection.getConnection();
String selectTableSQL = "select * from user_detail where username=? and password=?";
try {
stmt = connection.createStatement();
// execute select SQL stetement
ResultSet rs = stmt.executeQuery(selectTableSQL);
while (rs.next())
{
// get username and password from login page
String username = request.getParameter("password");
String password = request.getParameter("username");
System.out.println(username);
}
} catch (SQLException e) 
{
// TODO Auto-generated catch block
e.printStackTrace();
}*/
String username = request.getParameter("Password");
String password = request.getParameter("Username");
System.out.println("username :-"+username);
System.out.println("password"+password);

}
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>SBTsystemAdminView</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>loginPage</servlet-name>
<servlet-class>com.Admin.main.AdminLogin</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginPage</servlet-name>
<url-pattern>/loginPage/*</url-pattern>
</servlet-mapping>
</web-app>

去掉空的service方法重写。它使servlet完全不做任何事情。阅读HTTPServlet有关服务方法的文档:

受保护的无效服务(HttpServlet请求req,HttpServlet响应响应抛出Servlet异常,java.io.io异常

从公共服务方法接收标准HTTP请求并将其发送到该类中定义的doXXX方法。此方法是特定于HTTP的版本服务(javax.Servlet.ServletRequest,javax.servlet.ServletResponse)方法没有必要覆盖这种方法

换句话说,这个方法的标准实现可以判断请求是GET还是POST,并路由到doGet或doPost函数。如果重写此方法,尤其是使用空白函数,则会阻止访问doGet和doPost函数。

最新更新