Introduction

Here we produce two JSP pages, in first the first (Display.jsp) it contains some data with a show all link. When the user clicks on that link, all related data of that link will be displayed in the second JSP page (showall.jsp).
Creation of a context file
Create any folder in any drive as (E:\Jsp). Inside that folder store your .jsp files. Give the Context pathname as JSP and docBase as E:\Jsp, here docBase means the total path where we are storing our .jsp files. These changes are done in the server.xml file, which is present in (E:\Program Files\Apache Software Foundation\Tomcat 6.0\conf) directory.
Server.xml file
  1. <?xml version='1.0' encoding='utf-8'?>
  2. <!--
  3. Licensed to the Apache Software Foundation (ASF) under one or more
  4. contributor license agreements. See the NOTICE file distributed with
  5. this work for additional information regarding copyright ownership.
  6. The ASF licenses this file to You under the Apache License, Version 2.0
  7. (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. -->
  15. <!-- Note: A "Server" is not itself a "Container", so you may not
  16. define subcomponents such as "Valves" at this level. Documentation at /docs/config/server.html
  17. -->
  18. <?xml version='1.0' encoding='utf-8'?>
  19. <!--
  20. Licensed to the Apache Software Foundation (ASF) under one or more
  21. contributor license agreements. See the NOTICE file distributed with
  22. this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
  23. http://www.apache.org/licenses/LICENSE-2.0
  24. Unless required by applicable law or agreed to in writing, software
  25. distributed under the License is distributed on an "AS IS" BASIS,
  26. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  27. See the License for the specific language governing permissions and
  28. limitations under the License.
  29. -->
  30. <!-- Note: A "Server" is not itself a "Container", so you may not
  31. define subcomponents such as "Valves" at this level.
  32. Documentation at /docs/config/server.html
  33. -->
  34. <Server port="8005" shutdown="SHUTDOWN">
  35. <!--APR library loader. Documentation at /docs/apr.html -->
  36. <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  37. <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
  38. <Listener className="org.apache.catalina.core.JasperListener" />
  39. <!-- JMX Support for the Tomcat server. Documentation at /docs/non-existent.html -->
  40. <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />
  41. <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  42. <!-- Global JNDI resources
  43. Documentation at /docs/jndi-resources-howto.html
  44. -->
  45. <GlobalNamingResources>
  46. <!-- Editable user database that can also be used by
  47. UserDatabaseRealm to authenticate users
  48. -->
  49. <Resource name="UserDatabase" auth="Container"
  50. type="org.apache.catalina.UserDatabase"
  51. description="User database that can be updated and saved"
  52. factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
  53. pathname="conf/tomcat-users.xml" />
  54. </GlobalNamingResources>
  55. <!-- A "Service" is a collection of one or more "Connectors" that share
  56. a single "Container" Note: A "Service" is not itself a "Container",
  57. so you may not define subcomponents such as "Valves" at this level.
  58. Documentation at /docs/config/service.html
  59. -->
  60. <Service name="Catalina">
  61. <!--The connectors can use a shared executor, you can define one or more named thread pools-->
  62. <!--
  63. <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
  64. maxThreads="150" minSpareThreads="4"/>
  65. -->
  66. <!-- A "Connector" represents an endpoint by which requests are received
  67. and responses are returned. Documentation at :
  68. Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
  69. Java AJP Connector: /docs/config/ajp.html
  70. APR (HTTP/AJP) Connector: /docs/apr.html
  71. Define a non-SSL HTTP/1.1 Connector on port 8080
  72. -->
  73. <Connector port="8081" protocol="HTTP/1.1"
  74. connectionTimeout="20000"
  75. redirectPort="8443" />
  76. <!-- A "Connector" using the shared thread pool-->
  77. <!--
  78. <Connector executor="tomcatThreadPool"
  79. port="8080" protocol="HTTP/1.1"
  80. connectionTimeout="20000"
  81. redirectPort="8443" />
  82. -->
  83. <!-- Define a SSL HTTP/1.1 Connector on port 8443
  84. This connector uses the JSSE configuration, when using APR, the
  85. connector should be using the OpenSSL style configuration
  86. described in the APR documentation -->
  87. <!--
  88. <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
  89. maxThreads="150" scheme="https" secure="true"
  90. clientAuth="false" sslProtocol="TLS" />
  91. -->
  92. <!-- Define an AJP 1.3 Connector on port 8009 -->
  93. <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
  94. <!-- An Engine represents the entry point (within Catalina) that processes
  95. every request. The Engine implementation for Tomcat stand alone
  96. analyzes the HTTP headers included with the request, and passes them
  97. on to the appropriate Host (virtual host).
  98. Documentation at /docs/config/engine.html -->
  99. <!-- You should set jvmRoute to support load-balancing via AJP ie :
  100. <Engine name="Standalone" defaultHost="localhost" jvmRoute="jvm1">
  101. -->
  102. <Engine name="Catalina" defaultHost="localhost">
  103. <!--For clustering, please take a look at documentation at:
  104. /docs/cluster-howto.html (simple how to)
  105. /docs/config/cluster.html (reference documentation) -->
  106. <!--
  107. <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
  108. -->
  109. <!-- The request dumper valve dumps useful debugging information about
  110. the request and response data received and sent by Tomcat.
  111. Documentation at: /docs/config/valve.html -->
  112. <!--
  113. <Valve className="org.apache.catalina.valves.RequestDumperValve"/>
  114. -->
  115. <!-- This Realm uses the UserDatabase configured in the global JNDI
  116. resources under the key "UserDatabase". Any edits
  117. that are performed against this UserDatabase are immediately
  118. available for use by the Realm. -->
  119. <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
  120. resourceName="UserDatabase"/>
  121. <!-- Define the default virtual host
  122. Note: XML Schema validation will not work with Xerces 2.2.
  123. -->
  124. <Host name="localhost" appBase="webapps"
  125. xmlValidation="false" xmlNamespaceAware="false">
  126. <!-- SingleSignOn valve, share authentication between web applications
  127. Documentation at: /docs/config/valve.html -->
  128. <!--
  129. <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
  130. -->
  131. <!-- Access log processes all example.
  132. Documentation at: /docs/config/valve.html -->
  133. <!--
  134. <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
  135. prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/>
  136. <Context path="/jsp" docBase="E:\Jsp" reloadable="true" debug="0" />
  137. </Host>
  138. </Engine>
  139. </Service>
  140. </Server>
Table creation
First, create an employee table in Oracle and insert some data as below
  1. Create table employee(empid varchar(50),empname varchar(50),empaddress varchar(50),designation varchar(50),salary int)
  2. insert into employee values('E001','Raj','Delhi','Clerk',10000)
  3. insert into employee values('E002','Harry','Mumbai','Player',20000)
  4. insert into employee values('E003','Sunil','Noida','Producer',30000)
  5. insert into employee values('E004','Pollock','Durban','Manager',40000)
  6. insert into employee values('E005','Jonty','Perth','Director',50000)
  7. insert into employee values('E006','Kallis','Oslo','Dean',60000)
  8. insert into employee values('E007','Imran','Dubai','Admin',70000)
Creation of dsn (database source name)
Start-Control panel- Administrative Tools- Data Sources (ODBC)-go to system DSN tab-click add button-select a driver for which you want to set up a data source (for Oracle- Oracle in XE)-select it and click finish-give any name in data source name textbox-then click ok button.
Note:- Here Username=system, Password=pintu and Dsn name=dsn1
Display.jsp
  1. <%@ page import="java.sql.*" %>
  2. <
  3. html >
  4. <
  5. body bgcolor = "Yellow" >
  6. <
  7. table border = "1"
  8. width = "30%"
  9. height = "30%" >
  10. <
  11. tr >
  12. <
  13. th > < font color = 'Red' > EMP ID < /font></th >
  14. <
  15. th > < font color = 'Red' > EMP NAME < /font></th >
  16. <
  17. th > < font color = 'Red' > SALARY < /font></th >
  18. <
  19. th > < font color = 'Red' > SHOW ALL < /font></th >
  20. <
  21. /tr>
  22. <%
  23. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  24. Connection con=DriverManager.getConnection("jdbc:odbc:dsn1","system","pintu");
  25. Statement stmt=con.createStatement();
  26. ResultSet rs=stmt.executeQuery("select * from employee");
  27. while(rs.next())
  28. {
  29. String EmployeeId=rs.getString("empid");
  30. String EmployeeName=rs.getString("empname");
  31. String EmployeeAddress=rs.getString("empaddress");
  32. String Employeedesignation=rs.getString("designation");
  33. int Salary=rs.getInt("salary");
  34. %>
  35. <
  36. tr >
  37. <
  38. td > < b > < font color = '#663300' > <%=EmployeeId%> < /font></b > < /td> <
  39. td > < b > < font color = '#663300' > <%=EmployeeName%> < /font></b > < /td> <
  40. td > < b > < font color = '#663300' > <%=Salary%> < /font></b > < /td> <
  41. td > < b > < font color = '#663300' > < a href = 'showall.jsp?id=<%=EmployeeName%>' > Show All < /a></font > < /b></td >
  42. <
  43. /tr>
  44. <%
  45. }
  46. %>
  47. <
  48. /table> <
  49. /body> <
  50. /html>
showall.jsp
  1. <%@ page import="java.sql.*" %>
  2. <
  3. html >
  4. <
  5. body bgcolor = "Yellow" >
  6. <
  7. table border = "1"
  8. width = "30%"
  9. height = "30%" >
  10. <
  11. tr >
  12. <
  13. th > < font color = 'Red' > ID < /font></th >
  14. <
  15. th > < font color = 'Red' > NAME < /font></th >
  16. <
  17. th > < font color = 'Red' > ADDRESS < /font></th >
  18. <
  19. th > < font color = 'Red' > DESIGNATION < /font></th >
  20. <
  21. th > < font color = 'Red' > SALARY < /font></th >
  22. <
  23. /tr>
  24. <%
  25. String empname= request.getParameter("id");
  26. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  27. Connection con=DriverManager.getConnection("jdbc:odbc:dsn1","system","pintu");
  28. Statement stmt=con.createStatement();
  29. ResultSet rs=stmt.executeQuery("select * from employee where empname='"+empname+"'");
  30. while(rs.next())
  31. {
  32. String EmployeeId=rs.getString("empid");
  33. String EmployeeName=rs.getString("empname");
  34. String EmployeeAddress=rs.getString("empaddress");
  35. String Employeedesignation=rs.getString("designation");
  36. int Salary=rs.getInt("salary");
  37. %>
  38. <
  39. tr >
  40. <
  41. td > < b > < font color = '#663300' > <%=EmployeeId%> < /font></b > < /td> <
  42. td > < b > < font color = '#663300' > <%=EmployeeName%> < /font></b > < /td> <
  43. td > < b > < font color = '#663300' > <%=EmployeeAddress%> < /font></b > < /td> <
  44. td > < b > < font color = '#663300' > <%=Employeedesignation%> < /font></b > < /td> <
  45. td > < b > < font color = '#663300' > <%=Salary%> < /font></b > < /td> <
  46. /tr>
  47. <%
  48. }
  49. %>
  50. <
  51. /table> <
  52. /body> <
  53. /html>
Running the application
Run the tomcat then write the below link in the URL
http://localhost:8081/jsp/
Here JSP is the Context path, which we mentioned in the server.xml file, which is present in (E:\Program Files\Apache Software Foundation\Tomcat 6.0\conf) directory.
After giving the URL a set a listing will come, here only two appears as Display.jsp and showall.jsp click on Display.jsp.
Output
jdbc
Thanks for reading