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

时间:2014-02-19 09:25:10

标签: java jsp servlets

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

  

的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>

1 个答案:

答案 0 :(得分:0)

摆脱空service方法覆盖。它使servlet完全没有任何作用。阅读有关服务方法的documentation for HTTPServlet

  

protected void service(HttpServletRequest req,                          HttpServletResponse resp)                   抛出ServletException,                          java.io.IOException的

     

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

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