连接mysql时,Servlet没有响应任何内容

时间:2016-11-27 10:49:45

标签: html mysql database servlets

在这里,我创建了一个简单的HTML页面&转发给servlet.But 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

1 个答案:

答案 0 :(得分:0)

如下更改内容类型,

response.setContentType(“ text / plain”);