html - Servlet 在连接 mysql 时没有任何响应

标签 html mysql database servlets

在这里,我创建了一个简单的 HTML 页面并转发给 servlet。但是 servlet 没有显示任何内容。问题是,它没有显示任何内容。输出是..

The output show while calling the servlet html是否有问题

    My html code (login.html)    

        <!Doctype html>
        <head>
        <title>Login</title>
        <link rel="stylesheet" type="text/css" href="header.css"/>
        <style>
        .content{
        text-align:center;
        position:absolute;
        top:250px;
        left:450px;
        font-size:20px;
        color:white;
        }
        #inputbox
        {
        height:30px;
        width:300px;
        }
        #submit
        {
        color:white;
        height:40px;
        width:100px;
        position:absolute;
        top:150px;
        background: #E8F0F0;
        color:brown;
        border:none;
        }
        body
        {
        background:url(Images/loginbackground.jpg);
        }
        </style>
        </head>
        <body>
        <ol>
        <li><a href="mainpage.html">Home</a></li>
        <li><a href="courses.html">Courses</a></li>
        <li><a href="">Contact Us</a></li>
        <li id="signup"><a href="signup.html">Sign Up</a></li>
        </ol>
        <div class="content">
        <form method="post" action="LoginCheck">
        Username:<input type="text" name="username" placeholder="Enter your        userID" id="inputbox" required/><br><br>
    Password:<input type="password" name="password" placeholder="Enter Your password" id="inputbox" required/> <br><br>
    <input type="submit" name="login" value="login"  id="submit"/>
    </form>

    </div>
    </body>
    </html>

    Whether the servlet was correct
Servlet code is (LoginCheck.java)

import java.io.IOException;
import java.io.PrintWriter;

//import javax.servlet.RequestDispatcher;
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 java.sql.*;

/**
 * Servlet implementation class MySQLConnect
 */
@WebServlet("/LoginCheck") 
public class LoginCheck extends HttpServlet {

    private static final long serialVersionUID = 1L;


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        /*if((username.equalsIgnoreCase("jothibasu"))&&(password.equalsIgnoreCase("admin"))){  
            out.println("<div style='position: absolute; top:100px; left:400px;'>");
            out.println("<font size='15' color='blue'>Welcome "+username+" !</font>");
            out.println("</div>");
            RequestDispatcher rd=request.getRequestDispatcher("/courses.html");  
            rd.include(request, response); 
        }  
        else{  
            out.println("<div style='position: absolute; top:180px; left:400px;'>");
            out.println("<font size='15' color='darkgray'>Sorry UserName or Password Error!</font>");
            out.println("</div>");
            RequestDispatcher rd=request.getRequestDispatcher("/login.html");  
            rd.include(request, response);  

            }*/
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/startupdb", "root", "developer");
            PreparedStatement pst = conn.prepareStatement("Select username,userpassword from logindetials where username=? and userpassword=?");
            pst.setString(1, username);
            pst.setString(2, password);
            ResultSet rs = pst.executeQuery();
            if (rs.next()) {
                out.println("Correct login credentials");
            } 
            else {
                out.println("Incorrect login credentials");
            }
        } 
        catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }
}


And my database & table goes here

The detials of database

最佳答案

改变内容类型如下,

response.setContentType("text/plain");

关于html - Servlet 在连接 mysql 时没有任何响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40828075/

相关文章:

php - LEFT JOIN 与 WHERE 不起作用

mysql更新列删除最后一个字

html - 使用纯 CSS 的两列布局

javascript - Node.js - 连接到同一个服务器,同一个用户,使用多个 html 文件

php - 如果为空则显示提交按钮

database - 第三范式算法

sql - 主键也是星型模式中的外键。是好是坏?

stored-procedures - Cassandra中有存储过程的概念吗?

html - 手机上的背景图片看起来缩小了

html - 没有绝对位置的div中div的绝对位置