Connecting JSP To Mysql Database Lesson
Wall Script
Wall Script
Tuesday, September 16, 2008

Connecting JSP To Mysql Database Lesson

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>
</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
web notification

95 comments:

  1. wat about the login.html and reg.html for these........sri?

    ReplyDelete
  2. i want to have those yaar............

    plzz post those first sri......

    ReplyDelete
  3. .how to connect these jsp to html pages.........

    ReplyDelete
  4. hie srinivas,........plz explain how jsp and html pages get connected........plz post yaar.....!!

    ReplyDelete
  5. login.html and reg.html Code posted......

    ReplyDelete
  6. 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.

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

    ReplyDelete
  8. @Harini

    login.jsp include redirect .html

    request.getRequestDispatcher("/xyz.html");

    ReplyDelete
  9. Where is Download button :)

    ReplyDelete
  10. Thanks this Connecting JSP To Mysql Database Lesson very useful to me!!!!!!

    ReplyDelete
  11. 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

    ReplyDelete
  12. hi sir,
    how to run this project in glassfish server.
    i got the error as resources not available can u explan it clearly pls

    ReplyDelete
  13. 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...

    ReplyDelete
  14. 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

    ReplyDelete
  15. Thank you very much!

    ReplyDelete
  16. I have a problem in the code in line of connection
    DriverManager.getConnection("jdbc:mysql://localhost:3306..
    tell me the problem srinivas..

    ReplyDelete
    Replies
    1. my problem also not connected data base sir plz replay

      Delete
    2. Change the local host ID I.e; in the eg it is 3306.
      Kindly change it to your local host ID which will be by default 8080

      Delete
  17. 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. :)

    ReplyDelete
  18. 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:

    ReplyDelete
  19. 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

    ReplyDelete
  20. 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

    ReplyDelete
  21. 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

    ReplyDelete
  22. DUDE

    I DID A MISTAKE IN CODING

    In Login.html
    name="usr"
    login.jsp
    request.getParameter("user")

    usr & user?

    ReplyDelete
  23. nice presentation.......

    ReplyDelete
  24. 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....

    ReplyDelete
  25. thanks a lot very nicely explained

    ReplyDelete
  26. hi..Nice explanation.but how can i use both insert and retrive query in same jsp file ..Plz help me..im new to JSP

    ReplyDelete

  27. 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)

    ReplyDelete
  28. @Deepti

    Copy servlet.jar file into lib folder

    ReplyDelete
  29. 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...

    ReplyDelete
  30. I need to diplay login.jsp in login.html page only by clicking button please help me.

    ReplyDelete
  31. helped me lot 2 understand some basics

    ReplyDelete
  32. its nice working for me

    ReplyDelete
  33. Simple and easy way to understand...thanks I got what I wanted... excellent..!

    ReplyDelete
  34. 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???

    ReplyDelete
  35. 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

    ReplyDelete
  36. 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)

    ReplyDelete
  37. i want to crate login page and connnect badabase match username and password then open wellcome page in jsp. using jsp & servlet

    ReplyDelete
  38. 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.

    ReplyDelete
  39. thanks for the tutorial. i don't understand how it selects the database "test"

    ReplyDelete
  40. 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

    ReplyDelete
  41. 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

    ReplyDelete
  42. java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

    getting this error ...

    ReplyDelete
    Replies
    1. yeah..but when we do this in servlet..we use to do it in try/catch...this may help us to avoid exception

      Delete
  43. Thank you it was of really great help
    kudos
    NB if you have done everything and your site is giving sql errors please uplaod the mysql jar file in the libraries

    ReplyDelete
  44. very excellent site i never seen that . for me need
    accurately available in this site thank u anna by anand salem tamilnadu

    ReplyDelete
  45. Hi, This conntent is Realy good and well structured including the images. my name is JC from venezuela, im a Network Enginer and im starting in web app´s. My congratulations.

    ReplyDelete
  46. plz help me once i click on the login button
    the the code of login.jsp is displayed and the file doesnt runs

    ReplyDelete
  47. Those who who had the issues with the ..
    "java.lang.ClassNotFoundException: com.mysql.jdbc.Driver"

    errors and so on..

    YOu need to place the "mysql-jdbc.jar" file to the "/WEB-INF/lib" directory.

    restart tyour app server and try again..

    Also there is a typo error in login.html ..repalce "usr" with "user" ..

    ReplyDelete
  48. how you create above diagrams? using any software tool .....plz tell i want use in my paper persentaion PPT...

    ReplyDelete
  49. kudos !! nice tutorial here...
    thnxx

    ReplyDelete
  50. I GOT THIS ERROR:
    org.apache.jasper.JasperException: An exception occurred processing JSP page /project/insert.jsp at line 18

    15: user="root" password=""/>
    16:
    17:
    18:
    19: INSERT INTO Booking VALUES ('<%= request.getParameter("CompanyName") %>','<%= request.getParameter("BikeName")%>','<%= request.getParameter("CustomerName") %>','<%= request.getParameter("CustomerAddress") %>','
    20:
    21:

    ReplyDelete
  51. hello sir i am using mqsql 5.1 version . you dont tell how to connect with jsp to database. you tell to me connection procedure sir please.

    ReplyDelete
  52. hi i am ashok how to connect to jsp servlet and javabean

    ReplyDelete
  53. Hi I am shivshankar,
    Thank you for this tutorial but i got an error like following


    type Exception report

    message javax.servlet.ServletException: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown column 's' in 'field list'

    description The server encountered an internal error that prevented it from fulfilling this request.

    exception

    org.apache.jasper.JasperException: javax.servlet.ServletException: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown column 's' in 'field list'
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549)
    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:728)
    root cause

    javax.servlet.ServletException: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown column 's' in 'field list'
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:912)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:841)
    org.apache.jsp.reg_jsp._jspService(reg_jsp.java:86)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    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:728)
    root cause

    com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown column 's' in 'field list'
    com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
    com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2870)
    com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573)
    com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1665)
    com.mysql.jdbc.Connection.execSQL(Connection.java:3118)
    com.mysql.jdbc.Statement.executeUpdate(Statement.java:1313)
    com.mysql.jdbc.Statement.executeUpdate(Statement.java:1232)
    org.apache.jsp.reg_jsp._jspService(reg_jsp.java:77)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    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:728)
    note The full stack trace of the root cause is available in the Apache Tomcat/7.0.42 logs.

    ReplyDelete
  54. thank you sir for giving this information.........

    ReplyDelete
  55. Simple and effective!

    ReplyDelete
  56. what is username and pasword????

    ReplyDelete
  57. org.apache.jasper.JasperException: Unable to compile class for JSP:

    An error occurred at line: 23 in the jsp file: /login.jsp
    Syntax error on token "else", EmptyStatement expected after this token
    20: out.println("Invalid password try again");
    21: }
    22: }
    23: else
    24: %>
    HOW TO SOLVE THIS ERROR????

    ReplyDelete
  58. Hello Srinivas,

    I am big fan of your tutorial. I have started to learn JSP+Servlet. I am having problem to insert auto increment primary key. see my below code in two different format.

    Code-1
    -----------
    Class.forName(JDBC_DRIVER);
    // Open a connection
    out.println("Connecting to a selected database...");
    con = DriverManager.getConnection(DB_URL, USER, PASSWORD);
    out.println("Connected database successfully...");

    // Execute a query
    st = con.createStatement();
    sql = "insert into users (name,email,password) values ('" + name + "','" + email + "','" + password + "')";
    out.println(sql);
    int i = st.executeUpdate(sql);
    if (i > 0) {
    out.println("Inserted!!");
    }

    Code-2
    -----------
    Class.forName(JDBC_DRIVER);
    con = DriverManager.getConnection(DB_URL, USER, PASSWORD);
    PreparedStatement ps;
    st = con.createStatement();
    sql = "insert into users (name,email,password) values (?,?,?)";
    ps = con.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
    ps.setString(1, name);
    ps.setString(2, email);
    ps.setString(3, password);
    ps.executeUpdate();

    ReplyDelete
  59. keep files like this.
    program files->
    Apache tomcat software foundation->
    Tomcat7.0->
    webapps->create new folder(like test)
    keep files(.jsp and .html) in this folder->
    WEB-INF->
    .xml(web.xml)
    lib->.jar file(mysql-connector-java-5.1.18-bin)

    ReplyDelete
  60. could any one resolve my problem in the above program.....

    type Exception report

    message

    description The server encountered an internal error () that prevented it from fulfilling this request.

    exception

    org.apache.jasper.JasperException: javax.servlet.ServletException: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:548)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:456)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
    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:911)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:840)
    org.apache.jsp.class1.login_jsp._jspService(login_jsp.java:94)
    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:433)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
    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:1678)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)
    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:190)
    org.apache.jsp.class1.login_jsp._jspService(login_jsp.java:70)
    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:433)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
    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.22 logs.
    Apache Tomcat/7.0.22

    ReplyDelete
  61. plz post some other projects also like students' database for a collage

    ReplyDelete
  62. i cant able to connect database with jsp page .The fields which we enter are not getting connected to database so could u plz help me...

    ReplyDelete
  63. Hi

    Anyone who can help with this error, I would appreciate.

    org.apache.jasper.JasperException: An exception occurred processing JSP page /test/reg.jsp at line 10

    7: String fname=request.getParameter("fname");
    8: String lname=request.getParameter("lname");
    9: String email=request.getParameter("email");
    10: Class.forName("com.mysql.jdbc.Driver").newInstance();
    11: java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/users",
    12: "root","lehlo");
    13: Statement st= con.createStatement();


    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:405)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:349)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

    ReplyDelete
  64. thank for the post. :) Good post.
    is very useful and can be understood

    ReplyDelete
  65. where can i run this program

    ReplyDelete
  66. working fine. After registration page is not diverting to welcome.jsp . data is inserted successfully
    and. login.jsp is working fine

    ReplyDelete
  67. your code executed successfully. I put the welcome.jsp code "Registration is Successfull. Welcome to <%=user %>" below reg.jsp to get the desired result

    ReplyDelete
  68. HTTP Status 500 - The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application

    type Exception report

    message The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application

    description The server encountered an internal error that prevented it from fulfilling this request.

    exception

    org.apache.jasper.JasperException: The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application
    org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:56)
    org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:445)
    org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:117)
    org.apache.jasper.compiler.TagLibraryInfoImpl.generateTLDLocation(TagLibraryInfoImpl.java:325)
    org.apache.jasper.compiler.TagLibraryInfoImpl.(TagLibraryInfoImpl.java:154)
    org.apache.jasper.compiler.Parser.parseTaglibDirective(Parser.java:410)
    org.apache.jasper.compiler.Parser.parseDirective(Parser.java:475)
    org.apache.jasper.compiler.Parser.parseElements(Parser.java:1410)
    org.apache.jasper.compiler.Parser.parse(Parser.java:138)
    org.apache.jasper.compiler.ParserController.doParse(ParserController.java:242)
    org.apache.jasper.compiler.ParserController.parse(ParserController.java:102)
    org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:199)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:374)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:354)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:341)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:657)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    note The full stack trace of the root cause is available in the Apache Tomcat/7.0.56 logs.

    ReplyDelete
  69. Replies
    1. it is not madatory to use web.xml .we can approach by using tightly coupled technique..

      Delete
  70. my problem is .jsp page not found and at com.dell.LoginServlet.doGet(LoginServlet.java:26)


    solve this problem

    ReplyDelete
  71. Working. Thanks for the post. Lot may get helped.

    ReplyDelete
  72. org.apache.jasper.JasperException: Unable to compile class for JSP:

    An error occurred at line: 8 in the jsp file: /jsp/sample1.jsp
    String literal is not properly closed by a double-quote
    5: session.putValue("user_id",userid);
    6: String pwd=request.getParameter("pwd");
    7: Class.forName("com.mysql.jdbc.Driver");
    8: java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/
    9: login","root","root");
    10: Statement st= con.createStatement();
    11: ResultSet rs=st.executeQuery("select * from users where user_id='"+userid+"'");


    Stacktrace:
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:366)
    org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:490)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:379)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:354)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:341)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:662)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:364)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

    note The full stack trace of the root cause is available in the Apache Tomcat/7.0.73 logs.

    pplz hlp

    ReplyDelete
  73. <%@page import="java.text.DateFormat"%>
    <%@page import="java.text.SimpleDateFormat"%>
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@page import= "java.sql.*" %>
    <%@page import="java.io.*" %>
    <%
    try
    {
    DriverManager.registerDriver(new com.mysql.jdbc.Driver());
    Connection cn=DriverManager.getConnection("jdbc:mysql://localhost:3306/amit","root","root");
    String sql="select *from students";
    PreparedStatement ps=cn.prepareStatement(sql);
    ResultSet rs=ps.executeQuery();

    while(rs.next())
    {

    out.println(rs.getString("rollno"));
    out.println(rs.getString("name"));
    out.println(rs.getString("course"));
    out.println();
    }

    }
    catch(Exception ex)
    {
    out.println(ex.getMessage());
    }
    %>

    ReplyDelete
  74. Awesome, very helpful worked like a charm. Thanks for sharing

    ReplyDelete
  75. Very good description.

    But how can we link two pages I.e; after submitting the login/registration page it should be redirected to welcome page .
    How can we link them.

    Kindly requesting to solve my issue.

    Regards,
    Vineeth Raj.

    ReplyDelete
  76. how does request.getRequestDispatcher works?? I have included in the jsp file, but it doesnt work ??????

    ReplyDelete
  77. how does request.getRequestDispatcher works????? i have included in the jsp file, but it doesnt go from login page to reg page

    ReplyDelete
  78. i think you can try in this way
    RequestDispatcher rs = request.getRequestDispatcher("login.html");
    rs.forward(request, response);

    ReplyDelete
  79. dont cleared how submit button works?

    ReplyDelete
  80. hello, whenever i fill the employee id i want the other details to be autofilled from the database.. help plz

    ReplyDelete

mailxengine Youtueb channel
Make in India
X