My brother Ravi Tamada request one mail about his college presentation. He is planning to do web dynamic project. So i am giving small explanation about JSP (Java Server Pages) to Mysql Connection structure, Tomcat directory structure and simple database examples.

Login.html

Code :
<body>
<form action="login.jsp" method="post">
User name :<input type="text" name="usr" />
password :<input type="password" name="pwd" />
<input type="submit" />
<form action="login.jsp" method="post">
User name :<input type="text" name="usr" />
password :<input type="password" name="pwd" />
<input type="submit" />
</form>
</body>Reg.html

code:
<form action="reg.jsp" method="post">
Email :<input type="text" name="email" />
First name :<input type="text" name="fname" />
Last name :<input type="text" name="lname" />
User name :<input type="text" name="userid" />
password :<input type="password" name="pwd" />
<input type="submit" />
</form>
Mysql Create Database Test:
Mysql no doubt about it best open source database http://mysql.com/

Create Table Users:

login.jsp
<%@ page import ="java.sql.*" %>
<%@ page import ="javax.sql.*" %>
<%
String userid=request.getParameter("user");
session.putValue("userid",userid);
String pwd=request.getParameter("pwd");
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/
test","root","root");
Statement st= con.createStatement();
ResultSet rs=st.executeQuery("select * from users where user_id='"+userid+"'");
if(rs.next())
{
if(rs.getString(2).equals(pwd))
{
out.println("welcome"+userid);
}
else
{
out.println("Invalid password try again");
}
}
else
%>
reg.jsp
<%@ page import ="java.sql.*" %>
<%@ page import ="javax.sql.*" %>
<%
String user=request.getParameter("userid");
session.putValue("userid",user);
String pwd=request.getParameter("pwd");
String fname=request.getParameter("fname");
String lname=request.getParameter("lname");
String email=request.getParameter("email");
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test",
"root","root");
Statement st= con.createStatement();
ResultSet rs;
int i=st.executeUpdate("insert into users values ('"+user+"','"+pwd+"','"+fname+"',
'"+lname+"','"+email+"')");
%>
welcome.jsp
<%@ page import ="java.sql.*" %>
<%@ page import ="javax.sql.*" %>
<%
String user=session.getValue("userid").toString();
%>
Registration is Successfull. Welcome to <%=user %>
Tomcat Directory Structure
Tomcat open source web server you can download from this link http://tomcat.apache.org/

Run Your Project









wat about the login.html and reg.html for these........sri?
i want to have those yaar............
plzz post those first sri......
.how to connect these jsp to html pages.........
hie srinivas,........plz explain how jsp and html pages get connected........plz post yaar.....!!
login.html and reg.html Code posted......
Simply stated, JDBC makes it possible to write platform independent Java programs that can be used to manipulate the data in a wide range of SQL databases without the requirement to modify and/or recompile the Java programs when moving from platform to platform or from DBMS to DBMS. MySQL is available for a wide variety of platforms.
its a nice post
very good this post. thanks
srinivas ur post was excellent..can you help me out with this..this user login when submit must go to another page that displays another html form.
@Harini
login.jsp include redirect .html
request.getRequestDispatcher("/xyz.html");
Where is Download button :)
Thanks this Connecting JSP To Mysql Database Lesson very useful to me!!!!!!
Hi Srinivas, I refer ur example for my application but i face on problem means getting an error so plz help me.
The exception is follow:
org.apache.jasper.JasperException: javax.servlet.ServletException: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
plz plz help me
Regards,
Mahesh
hi sir,
how to run this project in glassfish server.
i got the error as resources not available can u explan it clearly pls
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Login' did not find a matching property.
org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
.apache.catalina.startup.Catalina load
INFO: Initialization processed in 355 ms
Mar 13, 2012 11:48:26 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Mar 13, 2012 11:48:26 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.25
Mar 13, 2012 11:48:26 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Mar 13, 2012 11:48:26 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Mar 13, 2012 11:48:26 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 207 ms
Mar 13, 2012 11:48:34 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path [/Login] threw exception [An exception occurred processing JSP page /login.jsp at line 8
5: String userid=request.getParameter("user");
6: session.putValue("userid",userid);
7: String pwd=request.getParameter("pwd");
8: Class.forName("com.mysql.jdbc.Driver");
9: java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","dba");
10: Statement st= con.createStatement();
11: ResultSet rs=st.executeQuery("select * from users where user_id='"+userid+"'");
Stacktrace:] with root cause
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1701)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1546)
at org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:126)
at org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:63)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.apache.jsp.login_jsp._jspService(login_jsp.java:71)
at org.apache.jasper.runtime.HttpJspBase.servicee)
I m Facing this error anyone say me solutions...
hi, i highly appreciate the way you explain this connection but can you please explain how i can add date field and checkbox to my database and link it to a jsp page, the same way you explain the login process.
thanks
Thank you very much!
I have a problem in the code in line of connection
DriverManager.getConnection("jdbc:mysql://localhost:3306..
tell me the problem srinivas..
To people getting java.lang.ClassNotFoundException: com.mysql.jdbc.Driver you should download the jdbc mysql jar and add it to lib folder on your tomcat server.
Prashant Tyagi should see if his database is on and on port 3306. Mysql default port is 3386..
Glad to be of assistance. :)
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 25 in the jsp file: /reg.jsp
String literal is not properly closed by a double-quote
22: java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3386/test1");
23: Statement st=con.createStatement();
24: ResultSet rs;
25: int i=st.executeUpdate("insert into user values ('"+user+"','"+pwd+"','"+fname+"',
26: '"+lname+"','"+email+"')");
27:
28:
Can you solve my problem? after clicking submit button I get this:
" SEVERE: PWC6117: File "C:\...\build\web\login.jsp" not found "
login.jsp is in the same folder with login.html
Create a Table in Mysql database through SQL Query in JSP,this Connecting JSP To Mysql Database Lesson very useful, I need a program to provide download option for pdf file on my web page,the pdf file contains retrieved data from mysql table. I need this program by using jsp-servlets. any one can help me please
excellent
nice code thanks
My code does't work ,, hiks
wjhen i click on the submit button it goes to the login.jsp but it displays a blank page.
and yes i have used setAttribute instead of getValue
DUDE
I DID A MISTAKE IN CODING
In Login.html
name="usr"
login.jsp
request.getParameter("user")
usr & user?
nice presentation.......
Hey srinivas ,
how to assign a session variable to a normal variable,n print ??
And String user=session.getValue("userid").toString(); tis code not wrkin....
thanks a lot very nicely explained
hi..Nice explanation.but how can i use both insert and retrive query in same jsp file ..Plz help me..im new to JSP
I GOT THIS ERROR..MAY I HAVE SOLUTION FOR THIS....
org.apache.jasper.JasperException: An exception occurred processing JSP page /Register.jsp at line 12
9: String userid=request.getParameter("user");
10: session.putValue("user_id",userid);
11: String pwd=request.getParameter("pwd");
12: Class.forName("com.mysql.jdbc.Driver");
13: Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3386/tests","root","deepti");
14: Statement st= con.createStatement();
15: ResultSet rs=st.executeQuery("select * from users where user_id='"+userid+"'");
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
@Deepti
Copy servlet.jar file into lib folder
Hello srinivas,
I have added servlet.jar file into lib folder.. but again , same problem is here...
and
session.putValue("user_id",userid);
i got a warning in this line...
I need to diplay login.jsp in login.html page only by clicking button please help me.
helped me lot 2 understand some basics
really helpful
its nice working for me
Simple and easy way to understand...thanks I got what I wanted... excellent..!
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 15 in the jsp file: /reg.jsp
String literal is not properly closed by a double-quote
12: "root","tiger");
13: Statement st= con.createStatement();
14: ResultSet rs;
15: int i=st.executeUpdate("insert into users values ('"+user+"','"+pwd+"','"+fname+"',
16: '"+lname+"','"+email+"')");
17:
18:
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:102)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:331)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:469)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:378)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.27 logs.
what is the error???
The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 15 in the jsp file: /reg.jsp
String literal is not properly closed by a double-quote
12: "root","tiger");
13: Statement st= con.createStatement();
14: ResultSet rs;
15: int i=st.executeUpdate("insert into users values ('"+user+"','"+pwd+"','"+fname+"',
16: '"+lname+"','"+email+"')");
17:
18:
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:102)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:331)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:469)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:378)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.27 logs.
how to solve it
We compiled login.jsp and we have got the following errors. Kindly help me out.
compile-single-jsp:
init:
deps-module-jar:
deps-ear-jar:
deps-jar:
library-inclusion-in-archive:
library-inclusion-in-manifest:
compile:
Created dir: C:\Users\Administrator\Documents\NetBeansProjects\LoginTest\build\generated\src
Created dir: C:\Users\Administrator\Documents\NetBeansProjects\LoginTest\build\generated\classes
Compiling 1 source file to C:\Users\Administrator\Documents\NetBeansProjects\LoginTest\build\generated\classes
C:\Users\Administrator\Documents\NetBeansProjects\LoginTest\build\generated\src\org\apache\jsp\login_jsp.java:74: illegal start of expression
} catch (Throwable t) {
C:\Users\Administrator\Documents\NetBeansProjects\LoginTest\build\generated\src\org\apache\jsp\login_jsp.java:74: 'catch' without 'try'
} catch (Throwable t) {
C:\Users\Administrator\Documents\NetBeansProjects\LoginTest\build\generated\src\org\apache\jsp\login_jsp.java:74: ')' expected
} catch (Throwable t) {
C:\Users\Administrator\Documents\NetBeansProjects\LoginTest\build\generated\src\org\apache\jsp\login_jsp.java:74: not a statement
} catch (Throwable t) {
C:\Users\Administrator\Documents\NetBeansProjects\LoginTest\build\generated\src\org\apache\jsp\login_jsp.java:74: ';' expected
} catch (Throwable t) {
C:\Users\Administrator\Documents\NetBeansProjects\LoginTest\build\generated\src\org\apache\jsp\login_jsp.java:81: 'finally' without 'try'
} finally {
C:\Users\Administrator\Documents\NetBeansProjects\LoginTest\build\generated\src\org\apache\jsp\login_jsp.java:35: 'try' without 'catch' or 'finally'
try {
C:\Users\Administrator\Documents\NetBeansProjects\LoginTest\build\generated\src\org\apache\jsp\login_jsp.java:85: reached end of file while parsing
}
8 errors
C:\Users\Administrator\Documents\NetBeansProjects\LoginTest\nbproject\build-impl.xml:597: The following error occurred while executing this line:
C:\Users\Administrator\Documents\NetBeansProjects\LoginTest\nbproject\build-impl.xml:589: The following error occurred while executing this line:
C:\Users\Administrator\Documents\NetBeansProjects\LoginTest\nbproject\build-impl.xml:285: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 11 seconds)
i want to crate login page and connnect badabase match username and password then open wellcome page in jsp. using jsp & servlet
:D
Got a error like this
org.apache.jasper.JasperException: An exception occurred processing JSP page /verifyreg.jsp at line 28
25: String state=request.getParameter("cstate");
26: String coun=request.getParameter("country");
27:
28: Class.forName("com.mysql.jdbc.Driver");
29: java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
30: Statement st= con.createStatement();
31: ResultSet rs;
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause
javax.servlet.ServletException: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:912)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:841)
org.apache.jsp.verifyreg_jsp._jspService(verifyreg_jsp.java:110)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:126)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:63)
java.lang.Class.forName0(Native Method)
java.lang.Class.forName(Class.java:186)
org.apache.jsp.verifyreg_jsp._jspService(verifyreg_jsp.java:92)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.30 logs.
thanks for the tutorial. i don't understand how it selects the database "test"
Thank u soo much srinivas it works for me...
But in login.jsp code :
out.println(...) not working..
btw all its awesome! Thanks a Lot
I find this is great intro to JSP login with Mysql and Tomcat, I worked out the blank login.jsp page as Pankaj wrote earlier there.
What to do once register is done though, wouldn't it be useful to send back to the login page?
Also I wonder about the Welcome.jsp use as it's not linked to any of the other files..
I want to implement functions to allow user to input data in other tables so I need to know who is logged in to identify those inputs.
How can 'log out' function be implemented to complete the system?
Thanks for your input
good