用于检查用户名和密码的java Web应用程序存在于数据库中

时间:2013-06-24 07:59:33

标签: java mysql servlets

我是java的首发。 我正在尝试开发一个Web应用程序来验证mysql数据库中的用户名和密码。

当我运行程序时,我会遇到异常。任何人都可以帮我找出下面代码中的错误。

index.jsp
*********

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
      <form method="post" action="database">
             <body>
        <h1>Enter user details</h1>
        <h2>Name</h2>
        <input type="text" name="t1" /><br>
        <h2>Password</h2>
        <input type="password" name="p1" /><br><br>

            <input type="submit" value="Submit" name="b1">
            </body>
        </form>
</html>


c1.java
********


package model;

public class c1 {
    String name,password;

    public c1(String name, String password) {
        this.name = name;
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}


database.java
*************



package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;


public class database extends HttpServlet {
    @Resource(name = "db1")
    private DataSource db1;


    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String name=request.getParameter("t1").trim();
            String password=request.getParameter("p1").trim();



          int value=select(name,password);

              if(value==1)
              {

                    out.println("User details present in database");

              }
              else
              {
                    out.println(value);
                    out.println("ERROR Invalid user");
              }



        } 

  catch(Exception e2)
            {
                out.println("error");
            }
finally {            
            out.close();
        }
    }


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);`
    }


    @Override
    public String getServletInfo() {
        return "Short description";``
    }// </editor-fold>
public int select(String UserName,String Password) throws SQLException
   {
    DataSource d= db1;
       Connection c=d.getConnection();
       PreparedStatement p=c.prepareStatement
               ("select * from Employee where name='"+UserName+"' and password='"+Password+"'");

     ResultSet rs=p.executeQuery();
    int a= rs.getRow();
    return a;

   } 
}

1 个答案:

答案 0 :(得分:1)

您的代码中有几处错误:

JSP。身体标签的位置不正确:

正确的代码:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <form method="post" action="database">>
    <h1>Enter user details</h1>
    <h2>Name</h2>
    <input type="text" name="t1" />
    <br>
    <h2>Password</h2>
    <input type="password" name="p1" />
    <br>
    <br>

    <input type="submit" value="Submit" name="b1">
    </form>
</body>
</html>

JAVA。您的控制器中有几个错误。一些`字符产生错误。

正确的代码:

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
    processRequest(request, response); // ERROR Removed last `
}

@Override
public String getServletInfo() {
    return "Short description"; // ERROR removed lasts ``
}// </editor-fold>

WEB.XML:检查你的web.xml并确认你有下一行:

<servlet>
    <servlet-name>Database-servlet</servlet-name>
    <servlet-class>controller.database</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>Database-servlet</servlet-name>
    <url-pattern>/database</url-pattern>
</servlet-mapping>