Guest User

Untitled

a guest
Oct 21st, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.12 KB | None | 0 0
  1. 1. Create a servlet for a login page. If the username and password are correct then it says message “Hello ” else a message “login failed” D
  2.  
  3. Input.html
  4. <body>
  5. <form name=f1 method=get action="Login" >
  6. Enter UserId <input type="text" name="t1" ><br>
  7. Enter Password<input type="text" name="t2" ><br>
  8. <input type="submit" value="OK" >
  9. </form>
  10. </body>
  11. Login.java
  12. import java.io.*;
  13. import javax.servlet.*;
  14. import javax.servlet.http.*;
  15. public class Login extends HttpServlet
  16. {
  17. public void doGet(HttpServletRequest req,
  18. HttpServletResponse res) throws ServletException,IOException
  19. {
  20. PrintWriter pw=res.getWriter();
  21. String u= req.getParameter("t1");
  22. String p= req.getParameter("t2");
  23. if(u.equals("tyit") && p.equals("java"))
  24. {
  25. pw.println("Hello "+u);
  26. } else
  27. {
  28. pw.println("Login failed");
  29. }
  30. }
  31. }
  32.  
  33.  
  34. 2. Create a JSP application to demonstrate the use of JSTL. D
  35.  
  36.  
  37. index.jsp
  38.  
  39. <html>
  40. <head>
  41. <title>Index Page</title>
  42.  
  43. </head>
  44. <body>
  45. <form action="jstlDemo.jsp" method="post">
  46. First Name:<input type="text" name="fname"/><br/>
  47. Last Name:<input type="text" name="lname"/><br/>
  48. <input type="Submit" value="Check JSTL Demo"/>
  49.  
  50. </form>
  51. </body>
  52. </html>
  53.  
  54. jstlDemo.jsp
  55.  
  56. first download jstl from:https://www.javatpoint.com/jstl
  57.  
  58. save it in a loacatable drive
  59. copy paste it in WEB-INF folder &
  60. Add JAR file of jstl in Libraries
  61.  
  62. code:
  63. <%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
  64. <html>
  65. <head>
  66. <title>Welcome Page</title>
  67. </head>
  68. <body>
  69. First Name:<b> <c:out value="${param.fname}"></c:out></b><br>
  70. Last name <b> <c:out value="${param.lname}"></c:out></b><br>
  71. Use of if statement
  72. <br>
  73. <c:set var="mycount" value="25"/>
  74. <c:if test="${mycount == 25}">
  75. <b> <c:out value="Your count is 25"/> </b>
  76. </c:if>
  77. <br><br>
  78. Use of for Each statement
  79. <br>
  80. <c:forEach var="count" begin="101" end="105">
  81. <b> <c:out value="${count}"/></b>
  82. </c:forEach>
  83. <br><br>
  84. Exception catching Example
  85. <p>
  86. <c:catch var="myException">
  87. <%int number =10/0 ;%>
  88. </c:catch>
  89. <b> The Exception is ${myException}</b>
  90. </p>
  91. </body>
  92. </html>
  93.  
  94.  
  95. 3. Develop a simple JSP application to display values obtained from the use of implicit objects of various types D
  96.  
  97.  
  98. input.html
  99. <body>
  100. <form action="index.jsp">
  101. <input type="text" name="t1">
  102. <input type="submit" value =”ok”>
  103. </form>
  104. </body>
  105.  
  106. index.jsp
  107. <%
  108. String n=request.getParameter("t1");
  109. out.println(n);
  110. out.println(request.getMethod()+"<br/>");
  111. out.println(request.getRequestURL()+"<br/>");
  112. out.println(request.getQueryString()+"<br/>");
  113. response.setContentType("text/html");
  114. out.println(response.getCharacterEncoding()+"<br/>");
  115. out.println(response.getContentType()+"<br>");
  116. out.println(session.getId()+"<br/>");
  117. out.println(session.getCreationTime()+"<br/>");
  118. out.println(session.getLastAccessedTime()+"<br/>");
  119. %>
  120.  
  121.  
  122. 4. Develop simple application to demonstrate accessing Session Bean using EJB
  123.  
  124.  
  125. 5. Write a servlet code to store the biodata details of a user into the database. (Name, age, address, hobbies, gender, Qualification).
  126.  
  127. Index.jsp
  128. <html>
  129. <body>
  130. <form action="Bio" method="post">
  131. Enter name<input type="text" name="txt1">
  132. Enter age<input type="date" name="date">
  133. Enter Address<input type="text" name="txt2">
  134. Select Hobbies
  135. <input type="checkbox" name="hob" value="Singing">Singing
  136. <input type="checkbox" name="hob" value="Dancing">Dancing
  137. <input type="checkbox" name="hob" value="Reading">Reading
  138. Select Gender
  139. <input type="checkbox" name="g1" value="male">Male
  140. <input type="checkbox" name="g1" value="female">Female
  141. <input type="checkbox" name="g1" value="Other">Other
  142. Enter Qualification<input type="text" name="txt7">
  143. <input type="submit" value="submit">
  144. </form>
  145. </html>
  146.  
  147. Bio.java
  148. import java.io.IOException;
  149. import java.io.PrintWriter;
  150. import java.sql.Connection;
  151. import java.sql.DriverManager;
  152. import java.sql.PreparedStatement;
  153. import javax.servlet.ServletException;
  154. import javax.servlet.http.HttpServlet;
  155. import javax.servlet.http.HttpServletRequest;
  156. import javax.servlet.http.HttpServletResponse;
  157.  
  158. public class Bio extends HttpServlet {
  159.  
  160.  
  161. @Override
  162. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  163. throws ServletException, IOException {
  164. response.setContentType("text/html");
  165. PrintWriter out=response.getWriter();
  166. String n1=request.getParameter("txt1");
  167. String n2=request.getParameter("date");
  168. String n3=request.getParameter("txt2");
  169. String n4=request.getParameter("hob");
  170. String n5=request.getParameter("g1");
  171. String n6=request.getParameter("txt7");
  172. try {
  173.  
  174. Class.forName("com.mysql.jdbc.Driver");
  175.  
  176. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/logindb", "root", "root");
  177.  
  178. PreparedStatement ps = con.prepareStatement("insert into bio values(?,?,?,?,?,?)");
  179. ps.setString(1, n1);
  180. ps.setString(2, n2);
  181. ps.setString(3, n3);
  182. ps.setString(4, n4);
  183. ps.setString(5, n5);
  184. ps.setString(6, n6);
  185.  
  186. int i = ps.executeUpdate();
  187. if (i > 0);
  188. out.print("You are successfully registered...");
  189. } catch (Exception e2) {
  190. System.out.println(e2);
  191. }
  192. out.close();
  193. }
  194. }
  195.  
  196. 6. Develop simple EJB application to demonstrate Servlet Hit count using Singleton Session Beans. ///wrong
  197.  
  198. 7. Develop a JSP application to authenticate user login with database. D
  199. ADD JAR FILE OF MYSQL
  200. 1. Go to services – database
  201.  
  202. 2. right click on database - new connection
  203. in driver select Mysql Connector(connector/j driver).
  204. 3. Click on Next Give database name and root password.
  205. click on Test connection - next -finish
  206.  
  207. On Mysql
  208.  
  209. create database db1;
  210. use db1;
  211. create table user(username varchar(20),password varchar(20),emailid varchar(100));
  212. select * from user;
  213.  
  214. demo.html
  215. <body>
  216. <a href="input.html">Register new user</a>
  217. <a href="input1.html">Login</a>
  218. </body>
  219.  
  220. input.html
  221. <body>
  222. <form action="insert.jsp">
  223. User name<input type=text name=t1>
  224. Password<input type=password name=t2>
  225. Email ID<input type=text name=t3>
  226. <input type=submit value=ok>
  227. </form>
  228. </body>
  229.  
  230. insert.jsp
  231. <body>
  232. <%@ page import="java.sql.*"%>
  233. <%! String u,p,e;%>
  234. <%
  235. u=request.getParameter("t1");
  236. p=request.getParameter("t2");
  237. e=request.getParameter("t3");
  238. try
  239. {
  240. Class.forName("com.mysql.jdbc.Driver");
  241. Connection con=DriverManager.getConnection("jdbc:mysql://localhost/db1","root","abc");
  242. PreparedStatement pst=con.prepareStatement("insert into user values(?,?,?)");
  243. pst.setString(1,u);
  244. pst.setString(2,p);
  245. pst.setString(3,e);
  246. pst.execute();
  247. con.close();
  248. out.println("Data inserted successfully");
  249. }
  250. catch(Exception e)
  251. {
  252. out.println(e);
  253. }
  254. %>
  255. </body>
  256.  
  257. input1.html
  258. <body>
  259. <form action="verify.jsp">
  260. User name<input type=text name=t1>
  261. Password<input type=password name=t2>
  262. <input type=submit value=Login>
  263. </form>
  264. </body>
  265.  
  266. verify.jsp
  267. <%@ page import="java.sql.*"%>
  268. <%! String u,p;%>
  269. <%
  270. boolean f=false;
  271. u=request.getParameter("t1");
  272. p=request.getParameter("t2");
  273. try
  274. {
  275. Class.forName("com.mysql.jdbc.Driver");
  276. Connection con=DriverManager.getConnection("jdbc:mysql://localhost/db1","root","abc");
  277. Statement st=con.createStatement();
  278. ResultSet rs=st.executeQuery("select * from user");
  279. while(rs.next())
  280. {
  281. if(rs.getString("username").equals(u) && rs.getString("password").equals(p))
  282. {
  283. f=true;
  284. out.println("Login successful");
  285. break;
  286. }
  287. }
  288. if(f==false)
  289. {
  290. out.println("Invalid user");
  291. }
  292. }
  293. catch(Exception e)
  294. {
  295. out.println(e);
  296. }
  297. %>
  298.  
  299. 8. Create a JSP page to demonstrate the use of Expression language.
  300. Input.html
  301. <body>
  302. <form action="demo.jsp">
  303. Enter name <input type="text" name="t1">
  304. <input type="submit" value="ok">
  305. </form>
  306. </body>
  307. Demo.jsp
  308. <body>
  309. WELCOME ,${param.t1} <br>
  310. The sum is ${1+9} <br>
  311. The mul is ${1*9} <br>
  312. The sub is ${10-9} <br>
  313. The div is ${19/9}
  314.  
  315.  
  316. </body>
  317.  
  318. 9. Create a Servlet application to upload and download a file.
  319. File location should be as per pc
  320. Upload file example
  321.  
  322. Input.html
  323. <body>
  324. <form action="Upload" method="post" enctype="multipart/form-data">
  325. Select File to Upload:<input type="file" name="t1" >
  326. <input type="submit" value="ok">
  327. </form>
  328. </body>
  329.  
  330. Upload.java
  331. import java.io.*;
  332. import javax.servlet.*;
  333. import javax.servlet.annotation.*;
  334. import javax.servlet.http.*;
  335. @MultipartConfig
  336. public class Upload extends HttpServlet
  337. {
  338. public void doPost(HttpServletRequest req,HttpServletResponse res) throws
  339. ServletException, IOException
  340. {
  341. PrintWriter pw = res.getWriter();
  342. Part f=req.getPart("t1");
  343. try
  344. {
  345. FileOutputStream os=new FileOutputStream(new File("d:/abc.txt"));
  346. FileInputStream is=(FileInputStream)f.getInputStream();
  347. int r;
  348. while ((r = is.read()) != -1) {
  349. os.write((char)r);
  350. }
  351. pw.println("file uploaded sucessfully...!!!");
  352. } catch(Exception e)
  353. {
  354. pw.print(e);
  355.  
  356. }
  357. }
  358. }
  359.  
  360. Download file example
  361.  
  362. Input.html
  363. <body>
  364. <form action="Download" method="get" enctype="multipart/form-data">
  365. <input type="submit" value="ok">
  366. </form>
  367. </body>
  368.  
  369. Download.java
  370. import java.io.*;
  371. import javax.servlet.*;
  372. import javax.servlet.http.*;
  373. public class Download extends HttpServlet
  374. {
  375. public void doGet(HttpServletRequest req, HttpServletResponse res)throws
  376. ServletException, IOException
  377. {
  378. String filename="d:/abc.txt";
  379. FileInputStream is = new FileInputStream(filename);
  380. PrintWriter out=res.getWriter();
  381. res.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");
  382. int i;
  383. while ((i=is.read()) != -1) {
  384. out.write(i);
  385. }is.close();
  386. out.close();
  387. }
  388. }
  389.  
  390. 10. Create an html page with fields, eno, name, age, desg, salary. Now on submit this data to a JSP page which will update the employee table of database with matching eno.
  391.  
  392. On Mysql
  393. create database db2;
  394. use db2;
  395. create table emp(eno int,name varchar(20),age int,designation varchar(20),salary int);
  396. insert into emp values(1,'abc',35,'manager',30000);
  397. insert into emp values(2,'xyz',28,'TL',23000);
  398. insert into emp values(3,'pqr',40,'director',60000);
  399. select * from emp;
  400.  
  401. input.html
  402. <body>
  403. <form action="update.jsp">
  404. Emp no<input type=text name=t1>
  405. Name<input type=text name=t2>
  406. Age<input type=text name=t3>
  407. Designation<input type=text name=t4>
  408. Salary<input type=text name=t5>
  409. <input type=submit value=ok>
  410. </form>
  411. </body>
  412.  
  413. update.jsp
  414. <body>
  415. <%@ page import="java.sql.*"%>
  416. <%! int no,age,salary;String name,designation;%>
  417. <%
  418. no=Integer.parseInt(request.getParameter("t1");
  419. name=request.getParameter("t2");
  420. age=Integer.parseInt(request.getParameter("t3");
  421. designation=request.getParameter("t4");
  422. salary=Integer.parseInt(request.getParameter("t5");
  423. try
  424. {
  425. Class.forName("com.mysql.jdbc.Driver");
  426. Connection con=DriverManager.getConnection("jdbc:mysql://localhost/db2","root","abc");
  427. PreparedStatement pst=con.prepareStatement("update emp set
  428. name=?,age=?,designation=?,salary=? where eno=?");
  429. pst.setString(1,name);
  430. pst.setInt(2,age);
  431. pst.setString(3,designation);
  432. pst.setInt(4,salary);
  433. pst.setInt(5,no);
  434. pst.execute();
  435. con.close();
  436. out.println("Data updated ");
  437. }
  438. catch(Exception e)
  439. {
  440. out.println(e);
  441. }
  442. %>
  443. </body>
  444.  
  445. 11. Develop servlet application to demonstrate basic calculator. D
  446.  
  447. Input.html
  448. <body>
  449. <form action="Cal" method="get" name="frm">
  450. Enter Number 1:
  451. <input name="numl" type="text"/>
  452. Enter Number 2:
  453. <input name="num2" type="text" />
  454. Operator
  455. <select name="operator"/>
  456. <option value="Addition">+</option>
  457. <option value="Subtraction">-</option>
  458. <option value="Multiplication">*</option>
  459. <option value="division">/</option>
  460. </select>
  461. <input type="submit" value="submit"/>
  462. </form></body>
  463. Cal.java
  464. import java.io.*;
  465. import javax.servlet.*;
  466. import javax.servlet.http.*;
  467. public class Cal extends HttpServlet
  468. {
  469.  
  470. public void doGet(HttpServletRequest req,
  471. HttpServletResponse res) throws ServletException,IOException
  472. {
  473. PrintWriter out = res.getWriter();
  474. String num1 = req.getParameter("numl");
  475. String num2 = req.getParameter("num2");
  476. String op = req.getParameter("operator");
  477. if(op.equals("Addition")){
  478. out.println("Addition is :"+(Integer.parseInt(num1) + Integer.parseInt(num2)));
  479. }
  480. else if(op.equals("Subtraction"))
  481. {
  482. out.println("Subtraction is:"+ (Integer.parseInt(num1)-Integer.parseInt(num2)));
  483. }
  484. else if(op.equals("Multiplication")){
  485. out.println("Multiplication is:"+(Integer.parseInt(num1)*Integer.parseInt(num2)));
  486. }
  487. else{ out.println("Divisionis:"+(Integer.parseInt(num1)/Integer.parseInt(num2))); }}}
  488.  
  489. Using Request Dispatcher Interface create a Servlet which will validate the password entered by the user, if the user has entered "Servlet" as password, then he will be forwarded to Welcome Servlet else the user will stay on the index.html page and an error message will be displayed. D
  490.  
  491.  
  492. Input.html
  493. <body>
  494. <form name=f1 method=get action="Login" >
  495. Enter UserId <input type="text" name="t1" ><br>
  496. Enter Password<input type="text" name="t2" ><br>
  497. <input type="submit" value="OK" >
  498. </form>
  499. </body>
  500.  
  501. Login.java
  502. import java.io.*;
  503. import javax.servlet.*;
  504. import javax.servlet.http.*;
  505. public class Login extends HttpServlet
  506. {
  507. public void doGet(HttpServletRequest req,
  508. HttpServletResponse res) throws ServletException,IOException
  509. {
  510. PrintWriter pw=res.getWriter();
  511. String u= req.getParameter("t1");
  512. String p= req.getParameter("t2");
  513. if(u.equals("tyit") && p.equals("java"))
  514. {
  515. RequestDispatcher
  516. rd1=req.getRequestDispatcher("Result");
  517. rd1.forward(req,res);
  518. } else
  519. {
  520. RequestDispatcher
  521. rd2=req.getRequestDispatcher("input.html”);
  522. pw.println("Invalid user.Please try again");
  523. rd2.include(req,res);
  524. }
  525. }
  526. }
  527. Result.java
  528. import java.io.*;
  529. import javax.servlet.*;
  530. import javax.servlet.http.*;
  531. public class Result extends HttpServlet
  532. {
  533. public void doGet(HttpServletRequest req,
  534. HttpServletResponse res) throws ServletException,IOException
  535. {
  536. PrintWriter pw=res.getWriter();
  537. pw.println("Welcome");
  538. }
  539. }
  540.  
  541. 12. Create a JSP page to demonstrate the use of Expression language.
  542.  
  543.  
  544. 13. Develop a web application to allow the user to enter two numbers and select an operation
  545.  
  546. [ +,-,*,/ ] in an HTML file. When the user clicks “Calculate” button, call a servlet to compute the answer and display it to the user. D
  547.  
  548. index.html
  549. <body>
  550. <form action="Calculator" >
  551. Enter First Number <input type="text" name="t1" ><br>
  552. Enter Second Number <input type="text" name="t2" ><br>
  553. Select an Operation
  554. <input type="radio" name="r1" value="+">ADDTION
  555. <input type="radio" name="r1" value="-">SUBSTRACTION
  556. <input type="radio" name="r1" value="*">MULTIPLY
  557. <input type="radio" name="r1" value="/">DIVIDE <br>
  558. <input type="reset">
  559. <input type="submit" value="Calculate" >
  560. </form>
  561. </body>
  562. CalculatorServlet.java
  563. import java.io.*;
  564. import javax.servlet.*;
  565. import javax.servlet.http.*;
  566.  
  567. public class Calculator extends HttpServlet {
  568. public void doGet(HttpServletRequest request, HttpServletResponse response)
  569. throws ServletException, IOException
  570. {
  571. PrintWriter out = response.getWriter();
  572. out.println("Servlet Calculator Servlet");
  573. double n1 = Double.parseDouble(request.getParameter("t1"));
  574. double n2 = Double.parseDouble(request.getParameter("t2"));
  575. double result =0;
  576. String r1=request.getParameter("r1");
  577. if(r1.equals("+")) result=n1+n2;
  578. if(r1.equals("-")) result=n1-n2;
  579. if(r1.equals("*")) result=n1*n2;
  580. if(r1.equals("/")) result=n1/n2;
  581. out.println("<h1> Result = "+result);
  582. } }
  583.  
  584.  
  585. 14. Develop a simple JSP application to pass values from one page to another with validations. (Name-txt, age-txt, hobbies-checkbox, email-txt, gender-radio button). D
  586.  
  587.  
  588. input.html
  589. <body>
  590. <form action="validate.jsp">
  591. Enter Your Name<input type="text" name="name" ><br>
  592. Enter Your Age<input type="text" name="age" ><br>
  593. Enter Email<input type="text" name="email" ><br>
  594. Select Hobbies
  595. <input type="checkbox" name="hob" value="Singing">Singing
  596. <input type="checkbox" name="hob" value="Reading">Reading Books
  597. <input type="checkbox" name="hob" value="Football">Playing Football<br>
  598.  
  599. Select Gender
  600. <input type="radio" name="gender" value="male">Male
  601. <input type="radio" name="gender" value="female">Female
  602. <input type="radio" name="gender" value="other">Other<br>
  603. <input type="submit" value="Submit Form">
  604. </form>
  605. </body>
  606.  
  607. validate.jsp
  608. <body>
  609. <jsp:useBean id="obj" class="p1.Details" />
  610. <jsp:setProperty name="obj" property="*"/>
  611. <%
  612. if (obj.validate())
  613. {
  614. %>
  615. <jsp:forward page="s.jsp">
  616. <jsp:param name="p1" value="<%= obj.getName() %>" />
  617. <jsp:param name="p2" value="<%= obj.getAge() %>" />
  618. <jsp:param name="p3" value="<%= obj.getEmail() %>" />
  619. <jsp:param name="p4" value="<%= obj.getHob() %>" />
  620. <jsp:param name="p5" value="<%= obj.getGender() %>" />
  621. </jsp:forward>
  622. <%
  623. }
  624. else
  625. {
  626. %>
  627. <jsp:include page="input.html"/>
  628. <%= obj.getError() %>
  629. <%
  630. }
  631. %>
  632. </body>
  633.  
  634. </body>
  635.  
  636. Details.java
  637. package p1;
  638. public class Details
  639. {
  640. String name="", age="",hob="", email="", gender="", error="";
  641. public void setName(String n)
  642. {
  643. name=n;
  644. }
  645. public void setAge(String a)
  646. {
  647. age=a;
  648. }
  649. public void setHob(String h)
  650. {
  651. hob=h;
  652. }
  653. public void setEmail(String e)
  654. {
  655. email=e;
  656. }
  657. public void setGender(String g)
  658. {
  659. gender=g;
  660. }
  661. public void setError(String e)
  662. {
  663. error=e;
  664. }
  665. public String getName()
  666. {
  667. return name;
  668. }
  669. public String getAge()
  670. {
  671. return age;
  672. }
  673. public String getHob()
  674. {
  675.  
  676. return hob;
  677. }
  678. public String getEmail()
  679. {
  680. return email;
  681. }
  682. public String getGender()
  683. {
  684. return gender;
  685. }
  686. public String getError()
  687. {
  688. return error;
  689. }
  690. public boolean validate()
  691. {
  692. boolean res=true;
  693. if(name.equals(""))
  694. {
  695. error=error+"<br>Enter First Name";
  696. res=false;
  697. } if(age.equals("") )
  698. {
  699. error=error+"<br>Age Invalid";
  700. res=false;
  701. }
  702. return res;
  703. }
  704. }
  705.  
  706. s.jsp
  707. <body>
  708. Welcome, <%=request.getParameter("p1") %> <br>
  709.  
  710. <%=request.getParameter("p2") %> <br>
  711. <%=request.getParameter("p3") %><br>
  712. <%=request.getParameter("p4") %><br>
  713. <%=request.getParameter("p5") %>
  714.  
  715.  
  716. </body>
  717.  
  718.  
  719. 15. Write a JSP to accept eno, ename and salary. Insert these records in emp. (Create the emp table with these three fields.
  720.  
  721. Index.jsp
  722. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  723. <!DOCTYPE html>
  724. <html>
  725. <head>
  726. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  727. <title>JSP Registration</title>
  728. </head>
  729. <body>
  730. <h1>Registration Form</h1>
  731. <form action="val.jsp" method="post">
  732. <table style="width:50%">
  733. <tr>
  734. <td>Full Name</td>
  735. <td><input type="text" name="fullname"/></td>
  736. <td>age</td>
  737. <td><input type="text" name="age"></td>
  738.  
  739. <td>E-Mail</td><td><input type="text" name="email" size="20">
  740. </td></tr>
  741.  
  742. <tr> <td>Gender</td>
  743. <td><input type="radio" name="gender" value="Male">Male
  744. <input type="radio" name="gender" value="Female"> Female
  745. </td> </tr>
  746. <tr><td>Hobbies</td>
  747. <td>
  748. <input type="checkbox" name="hb" value="Acting">Acting
  749. <input type="checkbox" name="hb" value="Dancing"/>Dancing
  750. <input type="checkbox" name="hb" value="Singing"/>Singing
  751. </td> </tr>
  752. </table>
  753.  
  754. <input type="submit" value="Register">
  755. </form>
  756.  
  757. </body>
  758. </html>
  759.  
  760. val.jsp
  761. <%@page language="java" contentType="text/html" pageEncoding="UTF-8"%>
  762. <%!
  763. int ageInNumbers;
  764. private static final String EMAIL_REGEX = "^[\\w-\\+]+(\\.[\\w]+)*@[\\w]+(\\.[\\w]+)*(\\.[a-z]{2,})$";
  765. %>
  766. <%
  767. String name = request.getParameter("fullname");
  768. String age = request.getParameter("age");
  769. String email = request.getParameter("email");
  770. String gender = request.getParameter("gender");
  771. String hb[]= request.getParameterValues("hb");
  772.  
  773. if(name.isEmpty()|| age.isEmpty()|| email.isEmpty()|| gender.isEmpty())
  774. out.println("<font color=red>Please fill all the fields</font><br>");
  775.  
  776. if(!email.matches(EMAIL_REGEX)){
  777. out.println("<font color=red>Correct Your Email Address</font><br>");
  778. }
  779. try{
  780. ageInNumbers = Integer.parseInt(age.trim());
  781. }
  782. catch(NumberFormatException e){
  783. out.println("<font color=red>Age must be in numbers</font><br>");
  784. }
  785. if(ageInNumbers >= 18 && ageInNumbers >=60)
  786. {
  787. out.println("<font color=red>Age must be between 18 to 60</font><br>");
  788. }
  789. %>
  790. Your Entered Information is as follows: <br> <br>
  791. Full Name <b>:<%=name%> </b><br>
  792. Age <b>:<%=age%> </b><br>
  793. EMail <b>:<%=email%> </b><br>
  794. Gender <b>:<%= gender%> </b><br>
  795. Hobbies <b>:
  796. <%
  797. if (hb != null && hb.length !=0){
  798. for (int i = 0; i <hb.length; i++){
  799. out.println(hb[i]);
  800. }
  801. }
  802.  
  803. %>
  804.  
  805. web.xml
  806. <?xml version="1.0" encoding="UTF-8"?>
  807.  
  808. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  809. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  810. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  811. version="3.1">
  812. <welcome-file-list>
  813. <welcome-file>index.jsp</welcome-file>
  814. </welcome-file-list>
  815. <session-config>
  816. <session-timeout>
  817. 30
  818. </session-timeout>
  819. </session-config>
  820. </web-app>
  821.  
  822. 16. Write a JSP to accept eno, ename and salary. Insert these records in emp. (Create the emp table with these three fields.
  823.  
  824. Index.jsp
  825. <html>
  826. <body>
  827. <form action="Register" method="post">
  828. Eno.<input type="text" name="txt1">
  829. Ename.<input type="text" name="txt2">
  830. salary.<input type="text" name="txt3">
  831. <input type="submit" value="Submit" name="b1">
  832. </form>
  833.  
  834. </body>
  835. </html>
  836.  
  837. Register.java
  838.  
  839. import java.io.IOException;
  840. import java.io.PrintWriter;
  841. import java.sql.Connection;
  842. import java.sql.DriverManager;
  843. import java.sql.PreparedStatement;
  844. import javax.servlet.ServletException;
  845. import javax.servlet.http.HttpServlet;
  846. import javax.servlet.http.HttpServletRequest;
  847. import javax.servlet.http.HttpServletResponse;
  848. import java.sql.*;
  849.  
  850. public class Register extends HttpServlet {
  851.  
  852. @Override
  853. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  854. throws ServletException, IOException {
  855. response.setContentType("text/html");
  856. PrintWriter out = response.getWriter();
  857. String n = request.getParameter("txt1");
  858. String o = request.getParameter("txt2");
  859. String p = request.getParameter("txt3");
  860. try {
  861.  
  862. Class.forName("com.mysql.jdbc.Driver");
  863.  
  864. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/logindb", "root", "root");
  865.  
  866. PreparedStatement ps = con.prepareStatement("insert into emptable values(?,?,?)");
  867. ps.setString(1, n);
  868. ps.setString(2, o);
  869. ps.setString(3, p);
  870. int i = ps.executeUpdate();
  871. if (i > 0);
  872. out.print("You are successfully registered...");
  873. } catch (Exception e2) {
  874. System.out.println(e2);
  875. }
  876. out.close();
  877. }
  878. }
Add Comment
Please, Sign In to add comment