daily pastebin goal
67%
SHARE
TWEET

WT

a guest Apr 27th, 2016 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 1. Write a program to demonstrate HTML formatting tags
  2. <html>
  3. <head>
  4. <title>Basic</title>
  5. </head>
  6. <body>
  7. <h1> welcome </h1>
  8. <p><font size="18" color="red">this is <b>paragraph</b></p>
  9. </body>
  10. Output:
  11. 2. Write a program to to create table using HTML tags
  12. <HTML>
  13. <BODY>
  14. <TABLE Border = "1" Cellpadding = "5" Cellspacing = "5">
  15. <TR>
  16. <TH Colspan = "6" Align = "center">Time Table</TH>
  17. </TR>
  18. <TR>
  19. <TH Rowspan = "6">Hours</TH>
  20. <TH>Mon</TH>
  21. <TH>Tue</TH>
  22. <TH>Wed</TH>
  23. <TH>Thu</TH>
  24. <TH>Fri</TH>
  25. </TR>
  26. <TR>
  27. <TD>Science</TD>
  28. <TD>Maths</TD>
  29. <TD>Science</TD>
  30. <TD>Maths</TD>
  31. <TD>Arts</TD>
  32. </TR>
  33. <TR>
  34. <TD>Social</TD>
  35. <TD>History</TD>
  36. <TD>English</TD>
  37. <TD>Social</TD>
  38. <TD>Sports</TD>
  39. </TR>
  40. </TABLE>
  41. </BODY>
  42. </HTML>
  43. Output:
  44. 3. Write program to insert image using HTML tags
  45. <!DOCTYPE html>
  46. <html>
  47. <body>
  48. <h2>IMAGE</h2>
  49. <imgsrc="download.jpg" alt="Mountain View" style="width:304px;height:228px;">
  50. </body>
  51. </html>
  52. Output:
  53. 4. Write a program to insert links using HTML tags
  54. <html>
  55. <body>
  56. <p>links</p>
  57. <a href=”google.com”> click here to go to google</a>
  58. </body>
  59. </html>
  60. Output :
  61. 5. Write a program to display lists using HTML tags
  62. <html>
  63. <body>
  64. <h1> student skills</h1>
  65. <p>
  66. <ol>
  67. <li><p> programming languages</p>
  68. <ul>
  69. <li> C</li>
  70. <li>c++</li>
  71. <li>java</li>
  72. </ul>
  73. </li>
  74. <li><p> operating system</p>
  75. <ul>
  76. <li>linux</li>
  77. <li> windows</li>
  78. </ul>
  79. </ol>
  80. </p>
  81. </body>
  82. </html>
  83. Output:
  84. 6. Write a program to demonstrate the use of Marquee tag
  85. <html>
  86. <head>
  87. <title>HTML marquee Tag</title>
  88. </head>
  89. <body>
  90. <marquee direction="right" >cbit</marquee>
  91. <marquee scrolldelay=500 direction="right" >hyderabad</marquee>
  92. </body>
  93. </html>
  94. Output:
  95. 7. Write a program to demonstrate the use of CSS with div tag
  96. //Style.css
  97. <style>
  98. #section
  99. {
  100. width:350px;
  101. float:left;
  102. padding:10px;
  103. }
  104. #footer {
  105. background-color:black;
  106. color:white;
  107. clear:both;
  108. text-align:center;
  109. padding:5px;
  110. }
  111. .h {
  112. background-color:red;
  113. color:white;
  114. text-align:center;
  115. height:80px;
  116. }
  117. </style>
  118. //Css.hml
  119. <html>
  120. <link href="style.css" rel="stylesheet" type="text/css" media="all">
  121. <body>
  122. <div class="h">
  123. <h1>cbit</h1>
  124. </div>
  125. <div id="section">
  126. <p> web technology</p>
  127. </div>
  128. <div id="footer">
  129. lab
  130. </div>
  131. </body>
  132. </html>
  133. Output:
  134. 8. Create a valid and well formed XML document
  135. //College.dtd
  136. <!ELEMENT college (student,staff)>
  137. <!ELEMENT student (sid,sname)>
  138. <!ELEMENTsid (#PCDATA)>
  139. <!ELEMENTsname (#PCDATA)>
  140. <!ELEMENT staff (staffname,sid)>
  141. <!ELEMENTstaffid (#PCDATA)>
  142. <!ELEMENTstaffname (#PCDATA)>
  143. College.xml
  144. <?xml version="1.0" encoding="UTF-8"?>
  145. <!DOCTYPE note SYSTEM "college.dtd">
  146. <college>
  147. <student>
  148. <sid>101</sid>
  149. <sname>abc</sname>
  150. </student>
  151. <student>
  152. <sid>102</sid>
  153. <sname>def</sname>
  154. </student>
  155. <staff>
  156. <staffid>201</staffid>
  157. <staffname>xyz</staffname>
  158. </staff>
  159. <staff>
  160. <staffid>202</staffid>
  161. <staffname>ijk</staffname>
  162. </staff>
  163. </college>
  164. Output:
  165. 9. Create a valid and well formed XML document using XML Schema
  166. //Employee.xs
  167. <?xml version="1.0"?>
  168. <xs:schemaxmlns:xs="http://www.w3.org/2001/XMLSchema" >
  169. <xs:element name="Employee_Info" type="EmployeeInfoType" />
  170. <xs:complexType name="EmployeeInfoType">
  171. <xs:sequence>
  172. <xs:element ref="Employee" minOccurs="0" maxOccurs="unbounded" />
  173. </xs:sequence>
  174. </xs:complexType>
  175. <xs:element name="Employee" type="EmployeeType" />
  176. <xs:complexType name="EmployeeType">
  177. <xs:sequence>
  178. <xs:element ref="Name" />
  179. <xs:element ref="Department" />
  180. <xs:element ref="Telephone" />-
  181. <xs:element ref="Email" />
  182. </xs:sequence>
  183. <xs:attribute name="Employee_Number" type="xs:int" use="required"/>
  184. </xs:complexType>
  185. <xs:element name="Name" type="xs:string" />
  186. <xs:element name="Department" type="xs:string" />
  187. <xs:element name="Telephone" type="xs:string" />
  188. <xs:element name="Email" type="xs:string" />
  189. </xs:schema>
  190. //Employee.xml
  191. <?xml version="1.0"?>
  192. <Employee_Info
  193. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  194. xsi:noNamespaceSchemaLocation="employee.xs">
  195. <Employee Employee_Number="105">
  196. <Name>Masashi Okamura</Name>
  197. <Department>Design Department</Department>
  198. <Telephone>03-1452-4567</Telephone>
  199. <Email>okamura@xmltr.co.jp</Email>
  200. </Employee>
  201. <Employee Employee_Number="109">
  202. <Name>Aiko Tanaka</Name>
  203. <Department>Sales Department</Department>
  204. <Telephone>03-6459-98764</Telephone>
  205. <Email>tanaka@xmltr.co.jp</Email>
  206. </Employee>
  207. </Employee_Info>
  208. Output:
  209. 10. Create XSLT to display the contents of well formed XML document using for each iterator
  210. //College.xsl
  211. <?xml version="1.0" encoding="UTF-8"?>
  212. <xsl:stylesheet version="1.0"
  213. xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  214. <xsl:template match="/">
  215. <html>
  216. <body>
  217. <h2>COLLEGE DETAILS </h2>
  218. <h2>STUDENT DETAILS </h2>
  219. <table border="1">
  220. <trbgcolor="#9acd32">
  221. <th>STUDENT ID</th>
  222. <th>STUDENT NAMEt</th>
  223. </tr>
  224. <xsl:for-each select="college/student">
  225. <tr>
  226. <td><xsl:value-of select="sid"/></td>
  227. <td><xsl:value-of select="sname"/></td>
  228. </tr>
  229. </xsl:for-each>
  230. </table>
  231. <h2>STAFF DETAILS </h2>
  232. <table border="1">
  233. <trbgcolor="#9acd32">
  234. <th>STAFF ID</th>
  235. <th>STAFF NAMEt</th>
  236. </tr>
  237. <xsl:for-each select="college/staff">
  238. <tr>
  239. <td><xsl:value-of select="staffid"/></td>
  240. <td><xsl:value-of select="staffname"/></td>
  241. </tr>
  242. </xsl:for-each>
  243. </table>
  244. </body>
  245. </html>
  246. </xsl:template>
  247. </xsl:stylesheet>
  248. //College.xml
  249. <?xml version="1.0" encoding="UTF-8"?>
  250. <?xml-stylesheet type="text/xsl" href="college.xsl"?>
  251. <college>
  252. <student>
  253. <sid>101</sid>
  254. <sname>abc</sname>
  255. </student>
  256. <student>
  257. <sid>102</sid>
  258. <sname>def</sname>
  259. </student>
  260. <staff>
  261. <staffid>201</staffid>
  262. <staffname>xyz</staffname>
  263. </staff>
  264. <staff>
  265. <staffid>202</staffid>
  266. <staffname>ijk</staffname>
  267. </staff>
  268. </college>
  269. Output:
  270. 11. Create HTML document with all input types, validate user input using regular expressions and display the input provided by the user using Java script
  271. <html>
  272. <head>
  273. <title>Student Registration Form</title>
  274. <script language="javascript">
  275. function validate()
  276. {
  277. var x=document.getElementById("email").value;
  278. var y=/^[a-z0-9]{5,15}@[a-z]{5,15}\.[a-z]{3}$/;
  279. if(x.match(y))
  280. {
  281. alert("valid mail");
  282. returntrue;
  283. }
  284. else
  285. {
  286. alert("invalid mail");
  287. returnfalse;
  288. }
  289. }
  290. function display()
  291. {
  292. var x = document.getElementById("name").value;
  293. var y = document.getElementById("pass").value;
  294. var z = document.getElementById("sex").value;
  295. var mail=document.getElementById("email").value;
  296. var course1=document.getElementById("course").value;
  297. document.writeln("Name:"+x + "<br> Password:" + y + " <br> Sex:" + z + " <br> Email:" + mail + " <br> Course:" + course1);
  298. //document.getElementById("show").innerHTML = "Name:";
  299. }
  300. </script>
  301. </head>
  302. <body>
  303. <p>Name:</p>
  304. <input type="text" id="name" name="name">
  305. <br>
  306. <p>Password:</p>
  307. <input type="password" id="pass" name="pass">
  308. <p>Sex:</p>
  309. <p>Male
  310. <input type="radio" id="sex" name="sex" value="Male"></p>
  311. <p>Female
  312. <input type="radio" id="sex" name="sex" value="Female"></p>
  313. <br>
  314. <p>Email:</p>
  315. <input type="text" id="email" name="email">
  316. <br>
  317. <p>Course:<select id="course" name="course">
  318. <option value ="CSE">CSE</option>
  319. <option value="ECE">ECE</option>
  320. <option value="Mechanical">Mechanical</option>
  321. </select></p>
  322. <p id="show1"></p>
  323. <p id="show2"></p>
  324. <p id="show3"></p>
  325. <button type="submit"onclick=" validate(); display();">click</button>
  326. </body> </html>
  327. 12. Create HTML form with all input types, validate user input using regular expressions and display the input provided by the user in the current page using innerHTML
  328. <html>
  329. <head>
  330. <title>Student Registration Form</title>
  331. <script language="javascript">
  332. function validate()
  333. {
  334. var x=document.getElementById("email").value;
  335. var y=/^[a-z0-9]{5,15}@[a-z]{5,15}\.[a-z]{3}$/;
  336. if(x.match(y))
  337. {
  338. //document.getElementById("show").innerHTML = "valid";
  339. alert("valid mail")
  340. }
  341. else
  342. {
  343. alert("invalid mail");
  344. }
  345. }
  346. function display()
  347. {
  348. var x = document.getElementById("name").value;
  349. document.getElementById("show1").innerHTML = x;
  350. var y = document.getElementById("pass").value;
  351. document.getElementById("show2").innerHTML = y;
  352. var z = document.getElementsByName("Sex");
  353. for(vari=0;i<z.length;i++)
  354. {
  355. if(z[i].checked)
  356. document.getElementById("show3").innerHTML = z[i].value;
  357. }
  358. var s = document.getElementsByName("skills");
  359. var txt="";
  360. for(vari=0;i<s.length;i++)
  361. {
  362. if(s[i].checked)
  363. txt=txt+s[i].value+" ";
  364. }
  365. document.getElementById("show4").innerHTML = txt;
  366. var t = document.getElementById("Course").value;
  367. document.getElementById("show5").innerHTML = t;
  368. }
  369. functionmyFocus() {
  370. document.getElementById("email").style.backgroundColor = "yellow";
  371. }
  372. </script>
  373. </head>
  374. <body>
  375. <p>Name:</p>
  376. <input type="text" id="name">
  377. <p>Password:</p>
  378. <input type="password" id="pass" >
  379. <p>Email:</p>
  380. <input type="text" id="email" onfocus="myFocus()" onblur="validate()">
  381. <p>Sex:</p>
  382. <p>Male
  383. <input type="radio" name="Sex" value="Male" ></p>
  384. <p>Female
  385. <input type="radio" name="Sex" value="Female"></p>
  386. <p>Skills:</p>
  387. <input type="checkbox" name="skills" value="C" >C</p>
  388. <input type="checkbox" name="skills" value="C++" >C++</p>
  389. <input type="checkbox" name="skills" value="Java" >Java</p>
  390. <p>Course:<select id="Course">
  391. <option value ="CSE">CSE</option>
  392. <option value="ECE">ECE</option>
  393. <option value="Mechanical">Mechanical</option>
  394. </select></p>
  395. <button onclick="display()">click</button>
  396. <p id="show1"></p>
  397. <p id="show2"></p>
  398. <p id="show3"></p>
  399. <p id="show4"></p>
  400. <p id="show5"></p>
  401. </body>
  402. </html>
  403. 13. Create HTML form with all input types and display the input using Java servlet
  404. //Login.html
  405. <html>
  406. <body>
  407. <form action="http://localhost:8080/project/Login" method="GET">
  408. First Name: <input type="text" name="first_name">
  409. <br />
  410. Last Name: <input type="text" name="last_name" />
  411. <p>password:</p>
  412. <input type="password" name="pwd" />
  413. <p>gender:</p>
  414. <input type="radio" name="gender" value="male">
  415. <p>male</p>
  416. <input type="radio" name="gender" value="female">
  417. <p>female</p>
  418. <p>skills:</p>
  419. <input type="checkbox" name="c" checked="checked" />c
  420. <input type="checkbox" name="c++" />c++
  421. <input type="checkbox" name="java" checked="checked" />
  422. java
  423. <input type="submit" value="Submit" />
  424. </form>
  425. </body>
  426. </html>
  427. //Login.java:
  428. importjavax.servlet.*;
  429. importjavax.servlet.http.*;
  430. import java.io.*;
  431. public class Login extends HttpServlet
  432. {
  433. public void doGet(HttpServletRequestreq,HttpServletResponse res)throws IOException,ServletException
  434. {
  435. res.setContentType("text/html");
  436. PrintWriter pw=res.getWriter();
  437. String f=req.getParameter("first_name");
  438. String l=req.getParameter("last_name");
  439. pw.print("first name\n"+f);
  440. pw.print("last name"+l);
  441. String p=req.getParameter("pwd");
  442. pw.print("password"+p);
  443. String g=req.getParameter("gender");
  444. pw.print("gender"+g);
  445. String c1=req.getParameter("c");
  446. String c2=req.getParameter("c++");
  447. String c3=req.getParameter("java");
  448. pw.print("c"+c1+"c++"+c2+"java"+c3);
  449. pw.close();
  450. }
  451. }
  452. 14. Demonstrate Session Handling using cookies
  453. //Cookie.html
  454. <html>
  455. <head>
  456. <title>Cookie</title>
  457. </head>
  458. <body>
  459. <form action="http://localhost:8080/project1/CookieServlet1" method="post">
  460. Name:<input type="text" name="name">
  461. <input type="submit" value="submit">
  462. </form>
  463. </body>
  464. </html>
  465. //CookieServlet1.java
  466. import java.io.*;
  467. importjavax.servlet.*;
  468. importjavax.servlet.http.*;
  469. public class CookieServlet1 extends HttpServlet
  470. {
  471. public void doPost(HttpServletRequestreq, HttpServletResponse res)
  472. throwsServletException, IOException
  473. {
  474. res.setContentType("text/html");
  475. PrintWriter out = res.getWriter();
  476. String n=req.getParameter("name");
  477. out.print("Welcome "+n);
  478. Cookie ck=new Cookie("Name",n);
  479. res.addCookie(ck);
  480. out.print("Cookie created...");
  481. out.print("<form action='CookieServlet2' method='post'>");
  482. out.print("<input type='submit' value='submit'>");
  483. out.print("</form></html>");
  484. out.close();
  485. }
  486. }
  487. //CookieServlet2.java
  488. import java.io.*;
  489. importjavax.servlet.*;
  490. importjavax.servlet.http.*;
  491. public class CookieServlet2 extends HttpServlet
  492. {
  493. public void doPost(HttpServletRequestreq, HttpServletResponse res)throws ServletException, IOException
  494. {
  495. res.setContentType("text/html");
  496. PrintWriter out = res.getWriter();
  497. Cookie ck[]=req.getCookies();
  498. out.print("Hello "+ck[0].getValue());
  499. out.close();
  500. }
  501. }
  502. 15. Demonstrate Session Handling using URL rewriting
  503. //Index.html
  504. <html>
  505. <head>
  506. <h1> this is page one</h1>
  507. </head>
  508. <body>
  509. <form action="http://localhost:8080/projects/url1" method="get">
  510. <p>Name</p>
  511. <input typr="text" name="userName">
  512. <input type="submit" value="go">
  513. </form>
  514. </body>
  515. </html>
  516. // url1.java
  517. import java.io.*;
  518. importjavax.servlet.*;
  519. importjavax.servlet.http.*;
  520. public class url1 extends HttpServlet {
  521. public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException
  522. {
  523. response.setContentType("text/html");
  524. PrintWriter out = response.getWriter();
  525. String n=request.getParameter("userName");
  526. out.print("Welcome "+n);
  527. //appending the username in the query string
  528. out.print("<a href='servlet1?uname="+n+"'>visit</a>");
  529. out.close();
  530. }
  531. }
  532. // Servlet1.java
  533. import java.io.*;
  534. importjavax.servlet.*;
  535. importjavax.servlet.http.*;
  536. public class servlet1 extends HttpServlet {
  537. public void doGet(HttpServletRequest request, HttpServletResponse response)throws IOException,ServletException
  538. {
  539. response.setContentType("text/html");
  540. PrintWriter out = response.getWriter();
  541. String n=request.getParameter("uname");
  542. out.print("Hello "+n);
  543. out.close();
  544. }
  545. }
  546. 16. Demonstrate Session Handling using hidden form fields
  547. // Index.html
  548. <html>
  549. <head>
  550. <h1> this is page one</h1>
  551. </head>
  552. <body>
  553. <form action="Hidden" method="get">
  554. <p>Name</p>
  555. <input type="text" name="name">
  556. <input type="submit" value="go">
  557. </form>
  558. </body>
  559. </html>
  560. //Hidden.java
  561. importjavax.servlet.*;
  562. importjavax.servlet.http.*;
  563. import java.io.*;
  564. public class Hidden extends HttpServlet
  565. {
  566. public void doGet(HttpServletRequestreq,HttpServletResponse res)throws IOException,ServletException
  567. {
  568. res.setContentType("text/html");
  569. PrintWriter pw=res.getWriter();
  570. String z=req.getParameter("name");
  571. pw.print("<h1>welcome</h1>");
  572. pw.print("<form action='Servlet4' method='get'>");
  573. pw.print("<input type='hidden' name='name' value="+z+">");
  574. pw.print("<input type='submit' value='go'>");
  575. pw.print("</form></html>");
  576. pw.close();
  577. }
  578. }
  579. //Servlet4.java
  580. importjavax.servlet.*;
  581. importjavax.servlet.http.*;
  582. import java.io.*;
  583. public class Servlet4 extends HttpServlet
  584. {
  585. public void doGet(HttpServletRequestreq,HttpServletResponse res)throws IOException,ServletException
  586. {
  587. res.setContentType("text/html");
  588. PrintWriter pw=res.getWriter();
  589. String z=req.getParameter("name");
  590. pw.print("<h1>welcome to servlet 4 </h1>");
  591. pw.print("value is"+z);
  592. pw.close();
  593. }
  594. }
  595. 17. Demonstrate Session Handling using Http Session
  596. //Session1.java
  597. import java.io.*;
  598. importjavax.servlet.*;
  599. importjavax.servlet.http.*;
  600. publicclassSession1extendsHttpServlet {
  601. publicvoiddoGet(HttpServletRequest request, HttpServletResponse response)throws
  602. IOException,ServletException
  603. {
  604. response.setContentType("text/html");
  605. PrintWriter out = response.getWriter();
  606. //String n=request.getParameter("userName");
  607. out.print("Welcome ");
  608. HttpSession session=request.getSession();
  609. session.setAttribute("uname","cse-1");
  610. out.print("<a href='Servlet3'>visit</a>");
  611. out.close();
  612. }
  613. }
  614. //Servlet3.java
  615. import java.io.*;
  616. importjavax.servlet.*;
  617. importjavax.servlet.http.*;
  618. publicclassServlet3extendsHttpServlet {
  619. publicvoiddoGet(HttpServletRequest request, HttpServletResponse response)throwsIOException,ServletException
  620. {
  621. response.setContentType("text/html");
  622. PrintWriter out = response.getWriter();
  623. HttpSession session=request.getSession(false);
  624. String n=(String)session.getAttribute("uname");
  625. out.print("Hello "+n);
  626. out.close();
  627. }
  628. }
  629. 18. Demonstrate Session Handling using Servlet Context
  630. // Context.java
  631. importjavax.servlet.*;
  632. importjavax.servlet.http.*;
  633. import java.io.*;
  634. publicclassContextextendsHttpServlet
  635. {
  636. publicvoiddoGet(HttpServletRequestreq,HttpServletResponse res)throwsIOException,ServletException
  637. {
  638. res.setContentType("text/html");
  639. PrintWriter pw=res.getWriter();
  640. ServletContextsc=getServletContext();
  641. sc.setAttribute("company","IBM");
  642. pw.println("Welcome to first servlet");
  643. pw.println("<a href='Servlet2'>visit</a>");
  644. pw.close();
  645. }
  646. }
  647. // Servlet2.java
  648. importjavax.servlet.*;
  649. importjavax.servlet.http.*;
  650. import java.io.*;
  651. publicclassServlet2extendsHttpServlet
  652. {
  653. publicvoiddoGet(HttpServletRequestreq,HttpServletResponse res)throwsIOException,ServletException
  654. {
  655. res.setContentType("text/html");
  656. PrintWriter pw=res.getWriter();
  657. ServletContextsc=getServletContext();
  658. String n=(String)sc.getAttribute("company");
  659. pw.println("Welcome to "+n);
  660. pw.close();
  661. }
  662. }
  663. 19. Demonstrate Servlet Collaboration using Request Dispatcher
  664. //Index.html
  665. <html>
  666. <head>
  667. <h1> this is page one</h1>
  668. </head>
  669. <body>
  670. <form action="http://localhost:8080/projects/request" method="post">
  671. <p>Name</p>
  672. <input type="text" name="uname">
  673. <input type="text" name="password">
  674. <input type="submit" value="go">
  675. </form>
  676. </body>
  677. </html>
  678. //request.java
  679. importjavax.servlet.*;
  680. importjavax.servlet.http.*;
  681. import java.io.*;
  682. public class request extends HttpServlet
  683. {
  684. public void doPost(HttpServletRequestreq,HttpServletResponse res)throws IOException,ServletException
  685. {
  686. res.setContentType("text/html");
  687. PrintWriter pw=res.getWriter();
  688. String n= req.getParameter("uname");
  689. String p=req.getParameter("password");
  690. if(p.equals("servlet")){
  691. RequestDispatcher rd1 = req.getRequestDispatcher("reqdis.java");
  692. rd1.forward(req, res);}
  693. else
  694. {
  695. pw.println("Username or Password incorrect");
  696. RequestDispatcherrd = req.getRequestDispatcher("index.html");
  697. rd.include(req, res);
  698. }
  699. }
  700. }
  701. //reqdis.java
  702. importjavax.servlet.*;
  703. importjavax.servlet.http.*;
  704. import java.io.*;
  705. public class reqdis extends HttpServlet
  706. {
  707. public void doPost(HttpServletRequestreq,HttpServletResponse res)throws IOException,ServletException
  708. {
  709. res.setContentType("text/html");
  710. PrintWriter pw=res.getWriter();
  711. pw.println(“<h1>Welcome</h1>”);
  712. pw.close();
  713. }
  714. }
  715. 20. Demonstrate filters
  716. //counterfilter.java
  717. importjavax.servlet.*;
  718. importjavax.servlet.http.*;
  719. import java.io.*;
  720. public class counterfilter implements Filter
  721. {
  722. FilterConfigconfig;
  723. public void init(FilterConfigconfig)
  724. {
  725. this.config=config;
  726. }
  727. public void doFilter(ServletRequestreq, ServletResponseres,FilterChain chain) throws IOException,ServletException
  728. {
  729. ServletContext context=config.getServletContext();
  730. Integer count= (Integer) context.getAttribute("count");
  731. if(count==null)
  732. {
  733. count=new Integer(0);
  734. }
  735. count=new Integer(count.intValue()+1);
  736. context.setAttribute("count",count);
  737. chain.doFilter(req,res);
  738. }
  739. public void destroy()
  740. {}
  741. }
  742. //displaycount.java
  743. importjavax.servlet.*;
  744. importjavax.servlet.http.*;
  745. import java.io.*;
  746. public class displaycount extends HttpServlet
  747. {
  748. public void doGet(HttpServletRequestreq, HttpServletResponse res) throws IOException, ServletException
  749. {
  750. ServletContext context = getServletContext();
  751. Integer count= (Integer) context.getAttribute("count");
  752. res.setContentType("text/html");
  753. PrintWriter pw=res.getWriter();
  754. pw.println("<html>");
  755. pw.println("<meta http-equiv=\"Pragma\" content=\"no-cache\">");
  756. pw.println("<body>");
  757. if(count!=null)
  758. {
  759. pw.println(" the current count is " + count.intValue());
  760. }
  761. else
  762. {
  763. pw.println("count not available ");
  764. }
  765. pw.println("</body>");
  766. pw.println("</html>");
  767. pw.close();
  768. }
  769. }
  770. 21. Write a JDBC program to insert and extract details from database
  771. importjavax.sql.*;
  772. importjava.sql.*;
  773. public class JdbcDemo
  774. {
  775. public static void main(String[] a)
  776. {
  777. try
  778. {
  779. Class.forName("com.mysql.jdbc.Driver");
  780. System.out.println("connecting..");
  781. try
  782. {
  783. String uname="root";
  784. String pwd="root";
  785. String url="jdbc:mysql://localhost:3306/b";
  786. Connection conn=DriverManager.getConnection(url,uname,pwd);
  787. System.out.println("success");
  788. Statement stmt=conn.createStatement();
  789. stmt.execute("insert into student values('a',302)");
  790. ResultSetrs=stmt.executeQuery("select * from student");
  791. while(rs.next())
  792. {
  793. System.out.println(rs.getString(1)+"\t"+rs.getInt(2));
  794. }
  795. System.out.println("success");
  796. }
  797. catch(Exception e)
  798. {
  799. System.out.println(e);
  800. }
  801. }
  802. catch(ClassNotFoundException e)
  803. {
  804. System.out.println(e);
  805. }
  806. }
  807. }
  808. 22. Write a servlet program which inserts and extracts details from database table
  809. import java.io.*;
  810. importjavax.servlet.*;
  811. importjavax.servlet.http.*;
  812. importjavax.sql.*;
  813. importjava.sql.*;
  814. publicclassJdbcServletextendsHttpServlet {
  815. publicvoiddoGet(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException
  816. {
  817. response.setContentType("text/html");
  818. PrintWriter out =response.getWriter();
  819. try
  820. {
  821. Class.forName("com.mysql.jdbc.Driver");//System.out.println("connecting..");
  822. try
  823. {
  824. String uname="root";
  825. String pwd="root";
  826. String url="jdbc:mysql://localhost:3306/b";
  827. Connection conn=DriverManager.getConnection(url,uname,pwd);
  828. //out.println("success");
  829. Statement stmt=conn.createStatement();
  830. stmt.execute("insert into student values('c',207)");
  831. ResultSetrs=stmt.executeQuery("select * from student");
  832. while(rs.next())
  833. {
  834. out.print(rs.getString(1)+"\t"+rs.getInt(2));
  835. out.println("<br>");
  836. }
  837. //System.out.println("success");
  838. }
  839. catch(Exception e)
  840. {
  841. out.println(e);
  842. }
  843. }
  844. catch(ClassNotFoundException e)
  845. {
  846. out.println(e);
  847. }
  848. }
  849. }
  850. 23. Create HTML form with all input types and display input using JSP
  851. //Details.html <html>
  852. <head>
  853. <title>Student Registration Form</title>
  854. <script language="javascript">
  855. function validate()
  856. {
  857. var x=document.getElementById("email").value;
  858. var y=/^[a-z0-9]{5,15}@[a-z]{5,15}\.[a-z]{3}$/;
  859. if(x.match(y))
  860. {
  861. //document.getElementById("show").innerHTML = "valid";
  862. alert("valid mail")
  863. }
  864. else
  865. {
  866. alert("invalid mail");
  867. }
  868. } </script>
  869. </head>
  870. <body>
  871. <form action=”jsp1.jsp”>
  872. <p>Name:</p>
  873. <input type="text" id="name" name="name">
  874. <br>
  875. <p>Password:</p>
  876. <input type="password" id="pass" name="pass">
  877. <p>Sex:</p>
  878. <p>Male
  879. <input type="radio" id="sex" name="sex" value="Male"></p>
  880. <p>Female
  881. <input type="radio" id="sex" name="sex" value="Female"></p>
  882. <br>
  883. <p>Email:</p>
  884. <input type="text" id="email" name="email"onblur="validate()">
  885. <br>
  886. <p>Course:<select id="course" name="course">
  887. <option value ="CSE">CSE</option>
  888. <option value="ECE">ECE</option>
  889. <option value="Mechanical">Mechanical</option>
  890. </select></p>
  891. <p id="show1"></p>
  892. <p id="show2"></p>
  893. <p id="show3"></p>
  894. <button type="submit"onsubmit="display()">click</button>
  895. </form>
  896. </body>
  897. </html>
  898. // jsp1.jsp
  899. <%@ page import="java.io.*" %>
  900. <%@ page import="javax.servlet.*" %>
  901. <%@ page import="javax.servlet.http.*" %>
  902. <%
  903. String n=request.getParameter("name");
  904. String p=request.getParameter("pass");
  905. String s=request.getParameter("sex");
  906. String m=request.getParameter("email");
  907. String c=request.getParameter("course");
  908. out.println("Name: "+n);
  909. out.println("<br>");
  910. out.println("Password: "+p);
  911. out.println("<br>");
  912. out.println("Sex: "+s);
  913. out.println("<br>");
  914. out.println("Email: "+m);
  915. out.println("<br>");
  916. out.println("Course: "+c);
  917. %>
  918.  
  919.  
  920.  
  921.  
  922.  
  923.  
  924.  
  925. 9. Session Handling using cookies
  926. index.html
  927. <form method="post" action=" validate ">
  928. Name:<input type="text" name="user" /><br/>
  929. Password:<input type="text" name="pass" ><br/>
  930. <input type="submit" value="submit">
  931. </form>
  932. web.xml
  933. <webapp...>
  934. <servlet>
  935. <servletname>
  936. validate </servletname>
  937. <servletclass>
  938. MyServlet </servletclass>
  939. </servlet>
  940. <servletmapping>
  941. <servletname>
  942. validate </servletname>
  943. <urlpattern>/
  944. validate</urlpattern>
  945. </servletmapping>
  946. <servlet>
  947. <servletname>
  948. First </servletname>
  949. <servletclass>
  950. First </servletclass>
  951. </servlet>
  952. <servletmapping>
  953. <servletname>
  954. First </servletname>
  955. <urlpattern>/
  956. First</urlpattern>
  957. </servletmapping>
  958. <welcomefilelist>
  959. <welcomefile>
  960. index.html </welcomefile>
  961. </welcomefilelist>
  962. </webapp>
  963. MyServlet.java
  964. import java.io.*;
  965. import javax.servlet.*;
  966. import javax.servlet.http.*;
  967. public class MyServlet extends HttpServlet {
  968. protected void doPost (HttpServletRequest request, HttpServletResponse response)
  969. throws ServletException, IOException {
  970. response.setContentType("text/html;charset=UTF8");
  971. String name = request.getParameter(" user ");
  972. String pass = request.getParameter(" pass ");
  973. if(pass. equals ("1234"))
  974. {
  975. Cookie ck = new Cookie(" username ", name );
  976. response. addCookie ( ck );
  977. response.sendRedirect(" First ");
  978. }
  979. }
  980. }
  981. First.java
  982. import java.io.*;
  983. import javax.servlet.*;
  984. import javax.servlet.http.*;
  985. public class First extends HttpServlet {
  986. protected void doGet (HttpServletRequest request, HttpServletResponse response)
  987. throws ServletException, IOException {
  988. response.setContentType("text/html;charset=UTF8");
  989. PrintWriter out = response.getWriter();
  990. Cookie[] cks = request.getCookies();
  991. out.println("Welcome "+ cks[0].getValue() );
  992. }
  993. }
  994. 10.
  995. Session Hndling using URL Rewriting
  996. index.html
  997. <form method="post" action=" validate ">
  998. Name:<input type="text" name="user" /><br/>
  999. Password:<input type="text" name="pass" ><br/>
  1000. <input type="submit" value="submit">
  1001. </form>
  1002. web.xml
  1003. <webapp...>
  1004. <servlet>
  1005. <servletname>
  1006. validate </servletname>
  1007. <servletclass>
  1008. MyServlet </servletclass>
  1009. </servlet>
  1010. <servletmapping>
  1011. <servletname>
  1012. validate </servletname>
  1013. <urlpattern>/
  1014. validate</urlpattern>
  1015. </servletmapping>
  1016. <servlet>
  1017. <servletname>
  1018. First </servletname>
  1019. <servletclass>
  1020. First </servletclass>
  1021. </servlet>
  1022. <servletmapping>
  1023. <servletname>
  1024. First </servletname>
  1025. <urlpattern>/
  1026. First</urlpattern>
  1027. </servletmapping>
  1028. <welcomefilelist>
  1029. <welcomefile>
  1030. index.html </welcomefile>
  1031. </welcomefilelist>
  1032. </webapp>
  1033. MyServlet.java
  1034. import java.io.*;
  1035. import javax.servlet.*;
  1036. import javax.servlet.http.*;
  1037. public class MyServlet extends HttpServlet {
  1038. protected void doPost (HttpServletRequest request, HttpServletResponse response)
  1039. throws ServletException, IOException {
  1040. response.setContentType("text/html;charset=UTF8");
  1041. String name = request.getParameter(" user ");
  1042. String pass = request.getParameter(" pass ");
  1043. if(pass. equals ("1234"))
  1044. {
  1045. response.sendRedirect( "First?user_name="+ name +"" );
  1046. }
  1047. }
  1048. }
  1049. First.java
  1050. import java.io.*;
  1051. import javax.servlet.*;
  1052. import javax.servlet.http.*;
  1053. public class First extends HttpServlet {
  1054. protected void doGet (HttpServletRequest request, HttpServletResponse response)
  1055. throws ServletException, IOException {
  1056. response.setContentType("text/html;charset=UTF8");
  1057. PrintWriter out = response.getWriter();
  1058. String user = request.getParameter(" user_name ");
  1059. out.println("Welcome "+user);
  1060. }
  1061. }
  1062. 11. Session management using hidden form fields :
  1063. index.html
  1064. <form method="post" action=" validate ">
  1065. Name:<input type="text" name="user" /><br/>
  1066. Password:<input type="text" name="pass" ><br/>
  1067. <input type="submit" value="submit">
  1068. </form>
  1069. web.xml
  1070. <webapp...>
  1071. <servlet>
  1072. <servletname>
  1073. First </servletname>
  1074. <servletclass>
  1075. First </servletclass>
  1076. </servlet>
  1077. <servletmapping>
  1078. <servletname>
  1079. First </servletname>
  1080. <urlpattern>/
  1081. First</urlpattern>
  1082. </servletmapping>
  1083. <servlet>
  1084. <servletname>
  1085. Second </servletname>
  1086. <servletclass>
  1087. Second </servletclass>
  1088. </servlet>
  1089. <servletmapping>
  1090. <servletname>
  1091. Second </servletname>
  1092. <urlpattern>/
  1093. Second</urlpattern>
  1094. </servletmapping>
  1095. <welcomefilelist>
  1096. <welcomefile>
  1097. index.html </welcomefile>
  1098. </welcomefilelist>
  1099. </webapp>
  1100. First.java
  1101. import java.io.*;
  1102. import javax.servlet.*;
  1103. import javax.servlet.http.*;
  1104. public class First extends HttpServlet {
  1105. protected void doPost (HttpServletRequest request, HttpServletResponse response)
  1106. throws ServletException, IOException {
  1107. response.setContentType("text/html;charset=UTF8");
  1108. PrintWriter out = response.getWriter();
  1109. //getting value submitted in form from HTML file
  1110. String user = request.getParameter("user");
  1111. //creating a new hidden form field
  1112. out.println("<form action='Second'>");
  1113. out.println("<input type='hidden' name='user' value='"+ user +"'>");
  1114. out.println("<input type='submit' value='submit' >");
  1115. out.println("</form>");
  1116. }
  1117. }
  1118. Second.java
  1119. import java.io.*;
  1120. import javax.servlet.*;
  1121. import javax.servlet.http.*;
  1122. public class Second extends HttpServlet {
  1123. protected void doGet (HttpServletRequest request, HttpServletResponse response)
  1124. throws ServletException, IOException {
  1125. response.setContentType("text/html;charset=UTF8");
  1126. PrintWriter out = response.getWriter();
  1127. //getting parameter from the hidden field
  1128. String user = request.getParameter(" user ");
  1129. out.println("Welcome "+ user );
  1130. }
  1131. }
  1132. HTTP Session
  1133. 12. http://www.studytonight.com/servlet/httpsession.php
  1134. index.html
  1135. <form method="post" action=" Validate ">
  1136. User: <input type="text" name="user" /><br/>
  1137. Password: <input type="text" name="pass" ><br/>
  1138. <input type="submit" value="submit">
  1139. </form>
  1140. web.xml
  1141. <webapp..>
  1142. <servlet>
  1143. <servletname>
  1144. Validate </servletname>
  1145. <servletclass>
  1146. Validate </servletclass>
  1147. </servlet>
  1148. <servlet>
  1149. <servletname>
  1150. Welcome </servletname>
  1151. <servletclass>
  1152. Welcome </servletclass>
  1153. </servlet>
  1154. <servletmapping>
  1155. <servletname>
  1156. Validate</servletname>
  1157. <urlpattern>/
  1158. Validate</urlpattern>
  1159. </servletmapping>
  1160. <servletmapping>
  1161. <servletname>
  1162. Welcome</servletname>
  1163. <urlpattern>/
  1164. Welcome</urlpattern>
  1165. </servletmapping>
  1166. <welcomefilelist>
  1167. <welcomefile>
  1168. index.html </welcomefile>
  1169. </welcomefilelist>
  1170. </webapp>
  1171. Validate.java
  1172. import java.io.*;
  1173. import javax.servlet.*;
  1174. import javax.servlet.http.*;
  1175. public class Validate extends HttpServlet {
  1176. protected void doPost (HttpServletRequest request, HttpServletResponse
  1177. response)
  1178. throws ServletException, IOException {
  1179. response.setContentType("text/html;charset=UTF8");
  1180. String name = request.getParameter("user");
  1181. String pass = request.getParameter("pass");
  1182. if(pass. equals ("1234"))
  1183. {
  1184. //creating a session
  1185. HttpSession session = request.getSession();
  1186. session. setAttribute (" user ", name );
  1187. response. sendRedirect ("Welcome");
  1188. }
  1189. }
  1190. }
  1191. Welcome.java
  1192. import java.io.*;
  1193. import javax.servlet.*;
  1194. import javax.servlet.http.*;
  1195. public class Welcome extends HttpServlet {
  1196. protected void doGet (HttpServletRequest request, HttpServletResponse
  1197. response)
  1198. throws ServletException, IOException {
  1199. response.setContentType("text/html;charset=UTF8");
  1200. PrintWriter out = response.getWriter();
  1201. HttpSession session = request.getSession();
  1202. String user = (String)session.getAttribute(" user ");
  1203. out.println("Hello "+ user );
  1204. }
  1205. }
  1206. 13. Servelet Collaboration REquest
  1207. dispatch
  1208. http://www.javatpoint.com/requestdispatcherinservlet
  1209. index.html
  1210. <form action= "servlet1" method= "post" >
  1211. Name:<input type= "text" name= "userName" /><br/>
  1212. Password:<input type= "password" name= "userPass" /><br/>
  1213. <input type= "submit" value= "login" />
  1214. </form>
  1215. Login.java
  1216. import java.io.*;
  1217. import javax.servlet.*;
  1218. import javax.servlet.http.*;
  1219. public class Login extends HttpServlet {
  1220. public void doPost(HttpServletRequest request, HttpServletResponse response)
  1221. throws ServletException, IOException {
  1222. response.setContentType( "text/html" );
  1223. PrintWriter out = response.getWriter();
  1224. String n=request.getParameter( "userName" );
  1225. String p=request.getParameter( "userPass" );
  1226. if (p.equals( "servlet" ){
  1227. RequestDispatcher rd=request.getRequestDispatcher( "servlet2" );
  1228. rd.forward(request, response);
  1229. }
  1230. else {
  1231. out.print( "Sorry UserName or Password Error!" );
  1232. RequestDispatcher rd=request.getRequestDispatcher( "/index.html" );
  1233. rd.include(request, response);
  1234. }
  1235. }
  1236. }
  1237. WelcomeServlet.java
  1238. import java.io.*;
  1239. import javax.servlet.*;
  1240. import javax.servlet.http.*;
  1241. public class WelcomeServlet extends HttpServlet {
  1242. public void doPost(HttpServletRequest request, HttpServletResponse response)
  1243. throws ServletException, IOException {
  1244. response.setContentType( "text/html" );
  1245. PrintWriter out = response.getWriter();
  1246. String n=request.getParameter( "userName" );
  1247. out.print( "Welcome " +n);
  1248. }
  1249. }
  1250. web.xml<webapp>
  1251. <servlet>
  1252. <servletname>
  1253. Login</servletname>
  1254. <servletclass
  1255. >Login</servletclass
  1256. >
  1257. </servlet>
  1258. <servlet>
  1259. <servletname>
  1260. WelcomeServlet</servletname>
  1261. <servletclass
  1262. >WelcomeServlet</servletclass
  1263. >
  1264. </servlet>
  1265. <servletmapping>
  1266. <servletname>
  1267. Login</servletname>
  1268. <urlpattern>/
  1269. servlet1</urlpattern>
  1270. </servletmapping>
  1271. <servletmapping>
  1272. <servletname>
  1273. WelcomeServlet</servletname>
  1274. <urlpattern>/
  1275. servlet2</urlpattern>
  1276. </servletmapping>
  1277. <welcomefilelist>
  1278. <welcomefile>
  1279. index.html</welcomefile>
  1280. </welcomefilelist>
  1281. </webapp>
  1282. 14. Servlet Collab using Servlet context
  1283. 15. Filterss
  1284. http://www.javatpoint.com/servletfilter
  1285. 16. Jdbc for insert and extract details from a table
  1286. //STEP 1. Import required packages
  1287. import java . sql .*;
  1288. public class JDBCExample {
  1289. // JDBC driver name and database URL
  1290. static final String JDBC_DRIVER = "com.mysql.jdbc.Driver" ;
  1291. static final String DB_URL = "jdbc:mysql://localhost/STUDENTS" ;
  1292. // Database credentials
  1293. static final String USER = "username" ;
  1294. static final String PASS = "password" ;
  1295. public static void main ( String [] args ) {
  1296. Connection conn = null ;
  1297. Statement stmt = null ;
  1298. try {
  1299. //STEP 2: Register JDBC driver
  1300. Class . forName ( "com.mysql.jdbc.Driver" );
  1301. //STEP 3: Open a connection
  1302. System . out . println ( "Connecting to a selected database..." );
  1303. conn = DriverManager . getConnection ( DB_URL , USER , PASS );
  1304. System . out . println ( "Connected database successfully..." );
  1305. //STEP 4: Execute a query
  1306. System . out . println ( "Creating statement..." );
  1307. stmt = conn . createStatement ();
  1308. String sql = "SELECT id, first, last, age FROM Registration" ;
  1309. ResultSet rs = stmt . executeQuery ( sql );
  1310. //STEP 5: Extract data from result set
  1311. while ( rs . next ()){
  1312. //Retrieve by column name
  1313. int id = rs . getInt ( "id" );
  1314. int age = rs . getInt ( "age" );
  1315. String first = rs . getString ( "first" );
  1316. String last = rs . getString ( "last" );
  1317. //Display values
  1318. System . out . print ( "ID: " + id );
  1319. System . out . print ( ", Age: " + age );
  1320. System . out . print ( ", First: " + first );
  1321. System . out . println ( ", Last: " + last );
  1322. }
  1323. rs . close ();
  1324. } catch ( SQLException se ){
  1325. //Handle errors for JDBC
  1326. se . printStackTrace ();
  1327. } catch ( Exception e ){
  1328. //Handle errors for Class.forName
  1329. e . printStackTrace ();
  1330. } finally {
  1331. //finally block used to close resources
  1332. try {
  1333. if ( stmt != null )
  1334. conn . close ();
  1335. } catch ( SQLException se ){
  1336. } // do nothing
  1337. try {
  1338. if ( conn != null )
  1339. conn . close ();
  1340. } catch ( SQLException se ){
  1341. se . printStackTrace ();
  1342. } //end finally try
  1343. } //end try
  1344. System . out . println ( "Goodbye!" );
  1345. } //end main
  1346. } //end JDBCExample
  1347. 17. Servlet databse insert:
  1348. http://www.tutorialspoint.com/servlets/pdf/servletsdatabaseaccess.
  1349. pdf
  1350. // Loading required libraries
  1351. import java . io .*;
  1352. import java . util .*;
  1353. import javax . servlet .*;
  1354. import javax . servlet . http .*;
  1355. import java . sql .*;
  1356. public class DatabaseAccess extends HttpServlet {
  1357. public void doGet ( HttpServletRequest request ,
  1358. HttpServletResponse response )
  1359. throws ServletException , IOException
  1360. {
  1361. // JDBC driver name and database URL
  1362. static final String JDBC_DRIVER = "com.mysql.jdbc.Driver" ;
  1363. static final String DB_URL = "jdbc:mysql://localhost/TEST" ;
  1364. // Database credentials
  1365. static final String USER = "root" ;
  1366. static final String PASS = "password" ;
  1367. // Set response content type
  1368. response . setContentType ( "text/html" );
  1369. PrintWriter out = response . getWriter ();
  1370. String title = "Database Result" ;
  1371. String docType =
  1372. "<!doctype html public \"//
  1373. w3c//dtd html 4.0 " +
  1374. "transitional//en\">\n" ;
  1375. out . println ( docType +
  1376. "<html>\n" +
  1377. "<head><title>" + title + "</title></head>\n" +
  1378. "<body bgcolor=\"#f0f0f0\">\n" +
  1379. "<h1 align=\"center\">" + title + "</h1>\n" );
  1380. try {
  1381. // Register JDBC driver
  1382. Class . forName ( "com.mysql.jdbc.Driver" );
  1383. // Open a connection
  1384. Connection conn = DriverManager . getConnection ( DB_URL , USER , PASS );
  1385. // Execute SQL query
  1386. Statement stmt = conn . createStatement ();
  1387. String sql ;
  1388. sql = "SELECT id, first, last, age FROM Employees" ;
  1389. ResultSet rs = stmt . executeQuery ( sql );
  1390. // Extract data from result set
  1391. while ( rs . next ()){
  1392. //Retrieve by column name
  1393. int id = rs . getInt ( "id" );
  1394. int age = rs . getInt ( "age" );
  1395. String first = rs . getString ( "first" );
  1396. String last = rs . getString ( "last" );
  1397. //Display values
  1398. out . println ( "ID: " + id + "<br>" );
  1399. out . println ( ", Age: " + age + "<br>" );
  1400. out . println ( ", First: " + first + "<br>" );
  1401. out . println ( ", Last: " + last + "<br>" );
  1402. }
  1403. out . println ( "</body></html>" );
  1404. // Cleanup
  1405. environment
  1406. rs . close ();
  1407. stmt . close ();
  1408. conn . close ();
  1409. } catch ( SQLException se ){
  1410. //Handle errors for JDBC
  1411. se . printStackTrace ();
  1412. } catch ( Exception e ){
  1413. //Handle errors for Class.forName
  1414. e . printStackTrace ();
  1415. } finally {
  1416. //finally block used to close resources
  1417. try {
  1418. if ( stmt != null )
  1419. stmt . close ();
  1420. } catch ( SQLException se2 ){
  1421. } // nothing we can do
  1422. try {
  1423. if ( conn != null )
  1424. conn . close ();
  1425. } catch ( SQLException se ){
  1426. se . printStackTrace ();
  1427. } //end finally try
  1428. } //end try
  1429. }
  1430. }
  1431. Now let us compile above servlet and create following entries in web.xml
  1432. ....
  1433. <servlet>
  1434. < servlet name
  1435. > DatabaseAccess </ servlet name
  1436. >
  1437. < servlet class
  1438. > DatabaseAccess </ servlet class
  1439. >
  1440. </ servlet >
  1441. < servlet mapping
  1442. >
  1443. < servlet name
  1444. > DatabaseAccess </ servlet name
  1445. >
  1446. < url pattern
  1447. > /DatabaseAccess</ url pattern
  1448. >
  1449. </ servlet mapping
  1450. >
  1451. O/P :
  1452. ID: 100, Age: 18, First: Zara, Last: Ali
  1453. ID: 101, Age: 25, First: Mahnaz, Last: Fatma
  1454. ID: 102, Age: 30, First: Zaid, Last: Khan
  1455. ID: 103, Age: 28, First: Sumit, Last: Mittal
  1456. 18. HTML Form and retrieving inputs using jsp
  1457. What You'll Learn in This Hour:
  1458. ● How to use a JSP to receive form data
  1459. ● How to work with form parameters
  1460. ● How servlets interact with forms
  1461. ● How to use a single JSP to present a form and handle its submission
  1462. By now you are probably comfortable with the fundamentals of JavaServer Pages and servlets. In the
  1463. examples presented so far, you have learned how to send output back to the browser. One of the
  1464. advantages of the Web is that you can create forms to send data from the browser to the server. You are
  1465. finally ready to begin creating forms with JSP and servlets, and that's where the fun really begins!
  1466. A Simple HTML Form
  1467. To start off the exploration of HTML forms, it's best to start with a small form and expand from there. Also,
  1468. it's better to start with a JSP rather than a servlet, because it is easier to write out the HTML. Most of the
  1469. form handling for JSPs and servlets is identical, so after you know how to retrieve form information from a
  1470. JSP, you know how to do it from a servlet. Listing 3.1 shows an HTML file containing a simple input form
  1471. that calls a JSP to handle the form.
  1472. Listing 3.1 Source Code for SimpleForm.html
  1473. <html>
  1474. <body>
  1475. <h1>Please tell me about yourself</h1>
  1476. <form action="SimpleFormHandler.jsp" method="get">
  1477. Name: <input type="text" name="firstName">
  1478. <input type="text" name="lastName"><br>
  1479. Sex:
  1480. <input type="radio" checked name="sex" value="male">Male
  1481. <input type="radio" name="sex" value="female">Female
  1482. <p>
  1483. What Java primitive type best describes your personality:
  1484. <select name="javaType">
  1485. <option value="boolean">boolean</option>
  1486. <option value="byte">byte</option>
  1487. <option value="char" selected>char</option>
  1488. <option value="double">double</option>
  1489. <option value="float">float</option>
  1490. <option value="int">int</option>
  1491. <option value="long">long</option>
  1492. </select>
  1493. <br>
  1494. <input type="submit">
  1495. </form>
  1496. </body>
  1497. </html>
  1498. The <form> tag in Listing 3.1 sends the input from the form to a JSP called SimpleFormHandler.jsp . Figure
  1499. 3.1 shows this form running in a browser.
  1500. Figure
  1501. 3.1 HTML forms frequently serve as the front end for a JSP.
  1502. The SimpleFormHandler JSP does little more than retrieve the form variables and print out their values.
  1503. Listing 3.2 shows the contents of SimpleFormHandler.jsp , which you can see is pretty short.
  1504. Listing 3.2 Source Code for SimpleFormHandler.jsp
  1505. <html>
  1506. <body>
  1507. <%
  1508. // Grab the variables from the form.
  1509. String firstName = request.getParameter("firstName");
  1510. String lastName = request.getParameter("lastName");
  1511. String sex = request.getParameter("sex");
  1512. String javaType = request.getParameter("javaType");
  1513. %>
  1514. <%Print
  1515. out the variables. %>
  1516. <h1>Hello, <%=firstName%> <%=lastName%>!</h1>
  1517. I see that you are <%=sex%>. You know, you remind me of a
  1518. <%=javaType%> variable I once knew.
  1519. </body>
  1520. </html>
  1521. 19. JSP Tags’
  1522. Tag handler class:
  1523. A tag handler class should implement Tag / IterationTag / BodyTag interface or it can
  1524. also extend TagSupport / BodyTagSupport / SimpleTagSupport class. All the classes
  1525. that support custom tags are present inside javax.servlet.jsp.tagext . In the below
  1526. we are extending the class SimpleTagSupport .
  1527. Details.java
  1528. package beginnersbook.com;
  1529. import javax.servlet.jsp.tagext.*;
  1530. import javax.servlet.jsp.*;
  1531. import java.io.*;
  1532. public class Details extends SimpleTagSupport {
  1533. public void doTag() throws JspException , IOException {
  1534. /*This is just to display a message, when
  1535. * we will use our custom tag. This message
  1536. * would be displayed
  1537. */
  1538. JspWriter out = getJspContext().getOut();
  1539. out .println( "This is my own custom tag" );
  1540. }
  1541. }
  1542. TLD File
  1543. This file should present at the location: Project Name/WebContent/WEB‐INF/ and
  1544. it should have a .tld extension.
  1545. Note:
  1546. <name> tag: custom tag name. In this example we have given it as MyMsg
  1547. <tag‐class> tag: Fully qualified class name. Our tag handler class Details.java is in
  1548. package beginnersbook.com so we have given the value as
  1549. beginnersbook.com.Details .
  1550. message.tld
  1551. <taglib>
  1552. <tlibversion>
  1553. 1.0 </tlibversion>
  1554. <jspversion>
  1555. 2.0 </jspversion>
  1556. <shortname>
  1557. My Custom Tag </shortname>
  1558. <tag>
  1559. <name> MyMsg </name>
  1560. <tagclass>
  1561. beginnersbook.com.Details </tagclass>
  1562. <bodycontent>
  1563. empty </bodycontent>
  1564. </tag>
  1565. </taglib>
  1566. Using custom tag in JSP:
  1567. Above we have created a custom tag named MyMsg . Here we will be using it.
  1568. Note: taglib directive should have the TLD file path in uri field. Above we have
  1569. created the message.tld file so we have given the path of that file.
  1570. Choose any prefix and specify it in taglib directive’s prefix field. Here we have
  1571. specified it as myprefix .
  1572. Custom tag is called like this: <prefix:tagName/> . Our prefix is myprefix and tag
  1573. name is MyMsg so we have called it as <myprefix:MyMsg/> in the below JSP page.
  1574. <%@ taglib prefix= "myprefix" uri= "WEBINF/
  1575. message.tld" %>
  1576. <html>
  1577. <head>
  1578. <title> Custom Tags in JSP Example </title>
  1579. </head>
  1580. <body>
  1581. <myprefix:MyMsg/>
  1582. </body>
  1583. </html>
  1584. Output:
  1585. This is my own custom tag
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top