简体   繁体   中英

java web application for checking username and password exists in database

I am a starter in java. I am trying to develop a web application to validate user name and password in mysql database.

When i run the program i am getting exception. can anyone help me to find out what is wrong in the below code.

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;

   } 
}

You have several errors in your code:

JSP. Incorrect position of body tags:

Correct code:

<%@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. You have a couple of errors in your controller. Some ` characters producing errors.

Correct code:

@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: Check your web.xml and verify that you have the next lines:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM