Write a SERVLET application to accept username and password, search them into database, if found then display appropriate message on the browser otherwise display error message.

UserPass.html

<html>
<body>
<form method=post action="http://localhost:4141/Program/servlet/UserPass">
User Name :<input type=text name=user><br><br>
Password :<input type=text name=pass><br><br>
<input type=submit value="Login">
</form>
</body>
</html>

UserPass.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class UserPass extends HttpServlet
{
    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
            PrintWriter out = response.getWriter();
            try{
                        String us=request.getParameter("user");
                        String pa=request.getParameter("pass");
                         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                        Connection cn=DriverManager.getConnection("jdbc:odbc:dsn2","","");
                        Statement st=cn.createStatement();
                        ResultSet rs=st.executeQuery("select * from UserPass");
                        while(rs.next())
                      {
                        if(us.equals(rs.getString("user"))&&pa.equals(rs.getString("pass")))
                        out.println("Valid user");
                        else
                        out.println("Invalid user");
                      }
                }catch(Exception e)
                  {     
                      out.println(e);           
                  }
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
    {
        doGet(request, response);
    }
}

Web.xml file(servlet entry)

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>
<servlet>
        <servlet-name>UserPass</servlet-name>
        <servlet-class>UserPass</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>UserPass</servlet-name>
        <url-pattern>/servlet/UserPass</url-pattern>
    </servlet-mapping>
</web-app>

Comments

Popular posts from this blog

Write a java program to create Teacher table(TNo.TName, Sal, Desg) and insert a record in it.

Create a JSP page to accept a number from an user and display it in words: Example: 123 – One Two Three. The output should be in red color.

Write a multithreading program in java to display all the vowels from a given String.(Use Thread Class)