Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

login jsp example

Hi,

I am trying login html which leads to jsp page where it validates username and password. If it is correct should display valid user in jsp otherwise redirect to same login html similar to below

http://www.codemiles.com/jsp-examples/login-using-jsp-t3417.html

my code is
<form action="servlet1">  
Name:<input type="text" name="name"/><br/>  
Password:<input type="password" name="password"/><br/>  
  
<input type="submit" value="login">  
  
</form>  

Open in new window


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
            String username = request.getParameter("username");
            String password = request.getParameter("password");
           out.println("Checking login<br>");
            if (username == null || password == null) {
 
                out.print("Invalid paramters ");
            }
 
            // Here you put the check on the username and password
            if (username.toLowerCase().trim().equals("admin") && password.toLowerCase().trim().equals("admin")) {
                out.println("Welcome " + username + " <a href=\"index.jsp\">Back to main</a>");
                session.setAttribute("username", username);
            }
           else 
               {
                out.println("Invalid username and password");
           }
 
 
 
 
%> 
- See more at: http://www.codemiles.com/jsp-examples/login-using-jsp-t3417.html#sthash.CNDIshS7.dpuf
</body>
</html>

Open in new window


how to redirect login html to login.jsp and validate. please advise
ASKER CERTIFIED SOLUTION
Avatar of rrz
rrz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

http://www.java2s.com/Tutorial/Java/0360__JSP/Insertdatatoatable.htm

above worked fine
i got below output

Filling a Table


ID

Name

1  Tom  


ID

Name

2  Peter  

when i right clicked on Mod etc project and created new JSP it is creating under WebContent parallel to WEB-INF that is where we always supposeed to create JSPs and hTMLs?

my code is
<%@ page import="java.sql.*" %>
<HTML>
    <HEAD>
        <TITLE>Filling a Table</TITLE>
    </HEAD>

    <BODY>
        <H1>Filling a Table</H1>

        <%  
        
        
        
        
        String driver = "oracle.jdbc.driver.OracleDriver";
        String url = "jdbc:oracle:thin:@localhost:1521:XE";
        String username = "sample";
        String password = "admin";
        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url, username, password);     
    	Statement statement=conn.createStatement();
        
        
        
        
        
           /*  Connection connection = null;
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            connection = DriverManager.getConnection("jdbc:odbc:data", "YourName", "password");

            Statement statement = connection.createStatement(); */

            String command = "INSERT INTO Employees (ID, NAME) VALUES (1, 'Tom')";
            statement.executeUpdate(command);

            command = "INSERT INTO Employees (ID, NAME) VALUES (2, 'Peter')";
            statement.executeUpdate(command);

            ResultSet resultset = statement.executeQuery("select * from Employees");

            while(resultset.next()){ 
        %>
            <TABLE BORDER="1">
                <TR>
                    <TH>ID</TH>
                    <TH>Name</TH>
                </TR>
                <TR>
                    <TD> <%= resultset.getString(1) %> </TD>
                    <TD> <%= resultset.getString(2) %> </TD>
                </TR>
            </TABLE>
        <% 
            } 
        %>
    </BODY>
</HTML>

Open in new window


not html is there either
insert.png
Avatar of gudii9

ASKER

simple single jsp as above worked fine

if i try to put html like index.html or login.html and try to redirect to above jsp called InsertJSP.jsp then errors out

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<center>
		<h1>Banking System</h1>
		<form action="insertJSP.jsp" method="post">
			User Name: <input type="text" name="username" /><br />
			<br />EmailID: &nbsp;&nbsp;<input type="text" name="email" /><br />
			<br />
			<br /> <input type="submit" value="submit" />
		</form>
	</center>
</body>
</html>

Open in new window


above is login.html page

below is insertJSP.jsp code
<%@ page import="java.sql.*" %>
<HTML>
    <HEAD>
        <TITLE>Filling a Table</TITLE>
    </HEAD>

    <BODY>
        <H1>Filling a Table</H1>

        <%  
        
        
        
        
        String driver = "oracle.jdbc.driver.OracleDriver";
        String url = "jdbc:oracle:thin:@localhost:1521:XE";
        String username = "sample";
        String password = "admin";
        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url, username, password);     
    	Statement statement=conn.createStatement();
        
        
        
        
        
           /*  Connection connection = null;
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            connection = DriverManager.getConnection("jdbc:odbc:data", "YourName", "password");

            Statement statement = connection.createStatement(); */

            String command = "INSERT INTO Employees (ID, NAME) VALUES (1, 'Tom')";
            statement.executeUpdate(command);

            command = "INSERT INTO Employees (ID, NAME) VALUES (2, 'Peter')";
            statement.executeUpdate(command);

            ResultSet resultset = statement.executeQuery("select * from Employees");

            while(resultset.next()){ 
        %>
            <TABLE BORDER="1">
                <TR>
                    <TH>ID</TH>
                    <TH>Name</TH>
                </TR>
                <TR>
                    <TD> <%= resultset.getString(1) %> </TD>
                    <TD> <%= resultset.getString(2) %> </TD>
                </TR>
            </TABLE>
        <% 
            } 
        %>
    </BODY>
</HTML>

Open in new window


once i go to

http://localhost:65535/Mod/login.html

then at login page entering usernmae and password as  aa and aaa
gives below error
Stacktrace:] with root cause
java.sql.SQLSyntaxErrorException: ORA-00904: "PASSWORD": invalid identifier

      at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:440)
loginComesNotJSP.png
it is creating under WebContent parallel to WEB-INF that is where we always supposeed to create JSPs and hTMLs?
Yes.
I am not a SQL expert. So, I can't help you with the SQLSyntaxErrorException.
Avatar of gudii9

ASKER

now it kind of worked by going to server side jsp from html

my code is
login.html s

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<center>
		<h1>Banking System</h1>
		<form action="InsertJSP.jsp" method="post">
			User Name: <input type="text" name="userid" /><br />
			<br />EmailID: &nbsp;&nbsp;<input type="text" name="email" /><br />
			<br />
			<br /> <input type="submit" value="submit" />
		</form>
	</center>
</body>
</html>

Open in new window


i have typo in jsp name insertJSP i correced as InsterJSP
InsertJSp.jsp page code is
<%@ page import="java.sql.*" %>
<HTML>
    <HEAD>
        <TITLE>Filling a Table</TITLE>
    </HEAD>

    <BODY>
        <H1>Filling a Table</H1>

        <%  
        
        
        
        
        String driver = "oracle.jdbc.driver.OracleDriver";
        String url = "jdbc:oracle:thin:@localhost:1521:XE";
        String username = "sample";
        String password = "admin";
        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url, username, password);     
    	Statement statement=conn.createStatement();
        
        
        
        
        
           /*  Connection connection = null;
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            connection = DriverManager.getConnection("jdbc:odbc:data", "YourName", "password");

            Statement statement = connection.createStatement(); */

            String command = "INSERT INTO Employees (ID, NAME) VALUES (1, 'Tom')";
            statement.executeUpdate(command);

            command = "INSERT INTO Employees (ID, NAME) VALUES (2, 'Peter')";
            statement.executeUpdate(command);

            ResultSet resultset = statement.executeQuery("select * from Employees");

            while(resultset.next()){ 
        %>
            <TABLE BORDER="1">
                <TR>
                    <TH>ID</TH>
                    <TH>Name</TH>
                </TR>
                <TR>
                    <TD> <%= resultset.getString(1) %> </TD>
                    <TD> <%= resultset.getString(2) %> </TD>
                </TR>
            </TABLE>
        <% 
            } 
        %>
    </BODY>
</HTML>

Open in new window


now as attached when i enter
gp
and
gp@gp.com
as uer id and email i expected to get
valid user
but getting
invalid user
modIndexPage.png
invaldUSer.png
sampletable.png
Avatar of gudii9

ASKER

actually jsp name should be login.jsp i corrected as below

login.jsp is
<%@ page import="java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<% 
	







	String un=request.getParameter("username");
    String em=request.getParameter("email");

	 
    
    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@localhost:1521:XE";
    String username = "sample";
    String password = "admin";
    Class.forName(driver);
    Connection conn = DriverManager.getConnection(url, username, password);
  
	Statement st=conn.createStatement();
	
 
	
	String str_qry = "select * from SAMPLETABLE where name='"+un + "' AND email='"+em + "'";
	ResultSet rst=st.executeQuery(str_qry);
	
	
	
	if (!rst.next())
	{
		%>
		<h1> Invalid user </h1>
	<% }
	else { %>
		<h1> valid user </h1>
	<%   }
	conn.close();
	System.out.println("Connection closed...");

	

%>
</body>
</html>

Open in new window


login.html is

<form action="login">  
Name:<input type="text" name="username"/><br/>  
Email:<input type="text" name="email"/><br/>  
  
<input type="submit" value="login">  
  
</form>  

Open in new window


but when i run above project also.

do i need to configure web.xml

how htl know which jsp to go ?
how htl know which jsp to go ?
If you click on submit button in the form, then the browser will send request to address in action attribute.  
1:<form action="login"> 

Open in new window

What is login? I didn't see anything mapped to "login". Is it the same page that contains the form?  Are we making any progress? It seems that you keep changing to new code and I am confused.
do i need to configure web.xml
You have to map your Servlet. You have to map and register your Filter.  You can do that in your web.xml file  or with annotations.
Avatar of gudii9

ASKER

no servlet in this example only one html(login.html)and corresponding one jsp(login.jsp)

once user enter info in the html page goes to server side jsp and validates the data in database table(SAMPLETABLE) and if user exists in database in html page should say valid user otherwise say invalid user
Avatar of gudii9

ASKER

login.html  code is
<form action="InsertJSP_junk">  
Name:<input type="text" name="username"/><br/>  
Email:<input type="text" name="email"/><br/>  
  
<input type="submit" value="login">  
  
</form>  

Open in new window


why when i enter aa and aaaa as usernae and email going to below login.jsp and says invalid user
<%@ page import="java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<% 
	







	String un=request.getParameter("username");
    String em=request.getParameter("email");

	 
    
    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@localhost:1521:XE";
    String username = "sample";
    String password = "admin";
    Class.forName(driver);
    Connection conn = DriverManager.getConnection(url, username, password);
  
	Statement st=conn.createStatement();
	
 
	
	String str_qry = "select * from SAMPLETABLE where name='"+un + "' AND email='"+em + "'";
	ResultSet rst=st.executeQuery(str_qry);
	
	
	
	if (!rst.next())
	{
		%>
		<h1> Invalid user </h1>
	<% }
	else { %>
		<h1> valid user </h1>
	<%   }
	conn.close();
	System.out.println("Connection closed...");

	

%>
</body>
</html>

Open in new window


i expected instead to go to insertJSP_junk.jsp as below
<%@ page import="java.sql.*" %>
<HTML>
    <HEAD>
        <TITLE>Filling a Table</TITLE>
    </HEAD>

    <BODY>
        <H1>Filling a Table</H1>

        <%  
        
        
        
        
        String driver = "oracle.jdbc.driver.OracleDriver";
        String url = "jdbc:oracle:thin:@localhost:1521:XE";
        String username = "sample";
        String password = "admin";
        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url, username, password);     
    	Statement statement=conn.createStatement();
        
        
        
        
        
           /*  Connection connection = null;
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            connection = DriverManager.getConnection("jdbc:odbc:data", "YourName", "password");

            Statement statement = connection.createStatement(); */

            String command = "INSERT INTO Employees (ID, NAME) VALUES (1, 'Tom2')";
            statement.executeUpdate(command);

            command = "INSERT INTO Employees (ID, NAME) VALUES (2, 'Peter2')";
            statement.executeUpdate(command);

            ResultSet resultset = statement.executeQuery("select * from Employees");

            while(resultset.next()){ 
        %>
            <TABLE BORDER="1">
                <TR>
                    <TH>ID</TH>
                    <TH>Name</TH>
                </TR>
                <TR>
                    <TD> <%= resultset.getString(1) %> </TD>
                    <TD> <%= resultset.getString(2) %> </TD>
                </TR>
            </TABLE>
        <% 
            } 
        %>
    </BODY>
</HTML>

Open in new window

please advise
<form action="InsertJSP_junk">  

Open in new window

if you wanted to request insertJSP_junk.jsp when the form is submitted , then you should use
1:<form action="InsertJSP_junk.jsp">

Open in new window

Avatar of gudii9

ASKER

i used same one right?

<form action="InsertJSP_junk">  
Name:<input type="text" name="username"/><br/>  
Email:<input type="text" name="email"/><br/>  
  
<input type="submit" value="login">  
  
</form> 

Open in new window

<form action="InsertJSP_junk">

Open in new window

The only way that would work is for you to map something to InsertJSP_junk.  Why not just use InsertJSP_junk.jsp for the action?  If that is where you want to send the request?
Avatar of gudii9

ASKER

<form action="InsertJSP_junk">  
Name:<input type="text" name="username"/><br/>  
Email:<input type="text" name="email"/><br/>  
 
<input type="submit" value="login">  
 
</form>

as highlighted above i am pointing to same InsertJSP_junk.jsp but it is not going there but instead goes to login.jsp some reason? please advise
Avatar of gudii9

ASKER

The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path

first line of jsp says above error. what it means?
Avatar of gudii9

ASKER

http://j2eetutorials.50webs.com/JSP_example1.html

Open in new window


above example works correct

<html>
      <title>Sample Example </title>
<body>
      <h1> <center> Example of JSP </center> </h1>
      <b> Mathematics</b>
      <hr>
      <form method="post" action="a.jsp">
<font size=5 face="Times New Roman">
      <input type="radio" name="a1" value="add" checked>Addition</input><br>
      <input type="radio" name="a1" value="mul" >Multiplication</input><br>
      <input type="radio" name="a1" value="div" >Division</input><br>
</font>
<br><br>
Enter first Value &nbsp &nbsp &nbsp<input type="text" name="t1" value=""><br>
Enter second Value &nbsp<input type="text" name="t2" value=""><br>
<input type="submit" name="result">
      </form>
</body>
</html>

Open in new window


a.jsp is

<%@ page language="java"%>
<%@ page import="java.lang.*"%>
<html>
<body>
<H1><center>Result for <%=request.getParameter("a1")%></center></H1>
<%
int i=Integer.parseInt(request.getParameter("t1"));
int j=Integer.parseInt(request.getParameter("t2"));
int k=0;
String str=request.getParameter("a1");

if(str.equals("add"))
  k=i+j;
if(str.equals("mul"))
  k=i*j;
if(str.equals("div"))
  k=i/j;
%>
Result is <%=k%>
</body>
</html>

Open in new window


when i give 3 and 4 i got 7 as addition result



what is importance of below result? where we referred that?
<input type="submit" name="result">
as highlighted above i am pointing to same InsertJSP_junk.jsp but it is not going there but instead goes to login.jsp some reason?
I can't explain that. It doesn't make any sense to me. Maybe you need to clean and recompile your project. Something is mixed up.  
The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path

The servlet-api.jar file in Tomcat's lib folder needs to be on your build path.  
first line of jsp says above error. what it means?
Which line are you talking about?
what is importance of below result? where we referred that?
 <input type="submit" name="result">
In this case, they named the submit button "result". But they don't use the name for anything. When the submit button is clicked the request is made to the server.
Avatar of gudii9

ASKER

i will rerun soon and let you know soon in case of any issues still
Avatar of gudii9

ASKER

when i run login.html
<form action="InsertJSP_junk">  
Name:<input type="text" name="username"/><br/>  
Email:<input type="text" name="email"/><br/>  
  
<input type="submit" value="login">  
  
</form> 

Open in new window


it supposed to go below InsertJSP_junk with firstname and lastname right
<html>
<head>
<title>Using GET Method to Read Form Data</title>
</head>
<body>
<center>
<h1>Using GET Method to Read Form Data</h1>
<ul>
<li><p><b>First Name:</b>
   <%= request.getParameter("first_name")%>
</p></li>
<li><p><b>Last  Name:</b>
   <%= request.getParameter("last_name")%>
</p></li>
</ul>
</body>
</html>

Open in new window


but some how it goes to login.html with username and email

<form action="InsertJSP_junk">  
Name:<input type="text" name="username"/><br/>  
Email:<input type="text" name="email"/><br/>  
  
<input type="submit" value="login">  
  
</form> 

Open in new window


my package structure is as attached.

basically when i right click on project name TestJSP and say New HTML or New JSP it creates as in attachment jsp2.png file.

Do i supposed to right click on WebContent instead which i forgot.

when i right clicked on WebContent created html (NewFileWEbContent.html) it create under
WEB-INF unlike NewFile.html which was created when i right clicked on TestJSP project and did it.

what is difference between above two html in terms of location and accessability inside and outside that web application?
jsp1.png
jsp2.png
jsp3.png
Avatar of gudii9

ASKER

i think i got some clue when i enter gp and gp@gmail.com at login.html it redirected to
http://localhost:65535/TestJSP/InsertJSP_junk?username=gp&email=gp@gmail.com

i got 404 how to resolve?
HTTP Status 404 - /TestJSP/InsertJSP_junk


type Status report

message /TestJSP/InsertJSP_junk

description The requested resource is not available.
Avatar of gudii9

ASKER

I can't explain that. It doesn't make any sense to me. Maybe you need to clean and recompile your project. Something is mixed up.

how to ensure it goes to right jsp.

is there is a way i can put system,out.println(sysout) to check it goes to right jsp like regular java debugging putting some hard coded sysouts or some other way??
Avatar of gudii9

ASKER

as eclipise neon has issues with tomcat 9,8, 7


i downloaded eclipise mars.

when i say new server selected apache tomcat 7 then i saw option to install and dowload
and gave path      C:\gpJavaStuff      when i tried to start gave 8080 in use.

so i doulbe clicked server n eclipse once opened at top right hand corner i changed port to 9999            

below jsp application worked fine      
http://www.srccodes.com/p/article/2/JSP-Hello-World-Program-using-Eclipse-IDE-and-Tomcat-web-server
      http://localhost:9999/FinalWeb/helloworld.jsp
Avatar of gudii9

ASKER

i wrote login.html
<form action="helloworld">  
Name:<input type="text" name="username"/><br/>  
Email:<input type="text" name="email"/><br/>  
  
<input type="submit" value="login">  
  
</form> 

Open in new window


my helloworld.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello World - JSP tutorial</title>
</head>
<body>
    <%= "Hello World!" %>
</body>
</html>

Open in new window

i gave aa and aaaa in login.html but did not land in jsp page with 404 error.

do i need to configure jsp also in web,xml similar to servlet?
http://localhost:9999/FinalWeb/helloworld?username=aa&email=aaaa

HTTP Status 404 - /FinalWeb/helloworld


type Status report

message /FinalWeb/helloworld

description The requested resource is not available.
Avatar of gudii9

ASKER

checking here but no html with jsp example like my original question
https://anshuchoudhury.wordpress.com/category/sample-project-in-eclipse/
Avatar of gudii9

ASKER

when i right click and check jsp or html properties it shows whether it is under
WebContent/WEB-INF(here never getting created unless i move it to WEB_INF from WebContent)
or simply under WebContent(which is where it is created when i right click on project name and create new html)
when i right click on WebContent then also it creates parallel to WEB_INF similar to when i right click on project name and create new html step