a.Java application to demonstrate servlet life cycle.
b.Develop a servlet web application to create a login form which take input -Name, Phone number, Address, Language known(i.e., Hindi, Marathi, Kannada etc.) and hobbies(Reading, Travelling, Singing etc). Show the Entered values. c.Write a servlet program to find prime number and factorial. Create error page for factorial if negative number is entered as input.
c.Write a servlet program to find prime number and factorial. Create error page for factorial if negative number is entered as input.
--------------------------------------------------------------
a.Java application to demonstrate servlet life cycle.
CODE:
Index.html
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>Servlet Practical</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form name = "frm1" action = "servlet">
<label>Enter Your Name:</label>
<input type="text" name="txt1"/><br/>
<input type="submit" value="Click Me!"/>
</form>
</body>
</html>
servlet.java
package servpack;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class servlet extends HttpServlet {
String str1;
@Override
public void init(ServletConfig config) throws ServletException
{
str1 = " Welcome to servlet!";
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
String str2 = request.getParameter("txt1");
out.print("<h1>"+str2+str1+"<h1>");
//out.println("<h1>"+str1+"</h1>");
}
@Override
public void destroy(){
System.out.print("Done");
}
}
OUTPUT:
b.Develop a servlet web application to create a login form which take input -Name, Phone number, Address, Language known(i.e., Hindi, Marathi, Kannada etc.) and hobbies(Reading, Travelling, Singing etc). Show the Entered values.
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Servlet practical2</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form name ="frm" method = "post" action = "servletprac2">
<table border="1">
<thead>
<tr>
<th colspan="2"><h2>Enter Your Details:-</h2></th>
</tr>
</thead>
<tbody>
<tr>
<td>Enter Your Name:</td>
<td><input type = "text" name = "txt1"/></td>
</tr>
<tr>
<td>Enter Your Mobile No.</td>
<td><input type = "text" name="txt2"/></td>
</tr>
<tr>
<td>Enter Your Address:</td>
<td><textarea rows ="8" cols="30" name = "addr"></textarea></td>
</tr>
<tr>
<td><label>Language Known:</label></td>
<td>
<input type ="checkbox" name="ch1" value="English"/>English
<input type ="checkbox" name="ch1" value="Hindi"/>Hindi
<input type ="checkbox" name="ch1" value="Marathi"/>Marathi
<input type ="checkbox" name="ch1" value="Tamil"/>Tamil
<input type ="checkbox" name="ch1" value="Kannada"/>Kannada
</td>
</tr>
<tr>
<td><label>Hobbies:</label></td>
<td>
<select name="hob" multiple="multiple">
<option>Reading</option>
<option>Traveling</option>
<option>Swimming</option>
<option>Singing</option>
</select>
</td>
</tr>
<tr>
<td colspan="2"><center><input type="submit" name="btn1" value="Submit"/></center></td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
servletprac2.java
package pack2;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class servletprac2 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String name = request.getParameter("txt1");
String num = request.getParameter("txt2");
String add = request.getParameter("addr");
String lang[] = request.getParameterValues("ch1");
String hobi[] = request.getParameterValues("hob");
out.println("<h1>Entered values are</h1>");
out.println("<h2>Applicant Name:</h2>"+name);
out.println("<h2>Applicant address:</h2>"+add);
out.println("<h2>Applicant Mobile Number:</h2>"+num);
out.println("<h2>Langueges Known:</h2><h2>");
for(int i = 0;i<lang.length;i++){
out.print(lang[i]+"\t");
}
out.println("</h2>");
out.println("<h2>Hobbies</h2><h2>");
for(int i = 0;i<hobi.length;i++){
out.print(hobi[i]+"\t");
}
out.println("</h2>");
}
}
0 Comments