0% found this document useful (0 votes)
91 views7 pages

Final

The document contains 4 questions related to servlets and JSPs: 1. A servlet that displays the current date and time of the server. 2. A servlet that displays a welcome message for first time and returning visitors using their IP address and cookies. 3. A JSP page that accepts a number from the user and displays it in words. 4. A JSP program to perform arithmetic operations like addition, subtraction, multiplication and division using a HTML form to accept numbers and radio buttons for the operation. The result is displayed on another JSP page.

Uploaded by

sakshigadhe91
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views7 pages

Final

The document contains 4 questions related to servlets and JSPs: 1. A servlet that displays the current date and time of the server. 2. A servlet that displays a welcome message for first time and returning visitors using their IP address and cookies. 3. A JSP page that accepts a number from the user and displays it in words. 4. A JSP program to perform arithmetic operations like addition, subtraction, multiplication and division using a HTML form to accept numbers and radio buttons for the operation. The result is displayed on another JSP page.

Uploaded by

sakshigadhe91
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Q.1. Write a servlet program to display current date and time of server.

→DateSrv.java

import java.io.*;

import javax.servlet.*;

public class DateSrv extends GenericServlet

public void service(ServletRequestreq, ServletResponse res) throws IOException,


ServletException

res.setContentType("text/html");

PrintWriterpw = res.getWriter();

java.util.Date date = new java.util.Date();

pw.println("<h2>"+"Current Date & Time: " +date.toString()+"</h2>");

pw.close();

Q.2. Design a servlet to display "Welcome IP address of client" to first time visitor.
Display "Welcome-back IP address of client" if the user is revisiting the page. (Use
Cookies) (Hint: Use req.getRemote Addr() to get IP address of client)

→WelcomelP.java

import java.io.*;

import java.util.*;

import jakarta.servlet.*;

import jakarta.servlet.http.*;

public class WelcomeIP extends HttpServlet

Hashtable accesses = new Hashtable();


public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,
IOException {

res.setContentType("text/html");

Print Writer out = res.getWriter();

String remoteAddr = req.getRemote Addr();

if (remoteAddr = null) {

out.println("Welcome!");

Else{

out.println("IP address:" + remoteAddr + "!<br>");

Date lastAccess= (Date) accesses.get(remoteAddr);

if (lastAccess null) {

out.println("Welcome IP Address Of Client!");

Else{

out.println("Welcome-Back IP Address Of Client: ");

accesses.put(remoteAddr, new Date());

//...Continue handling the request...

3. Create a JSP page to accept a number from an user and display it in words: Example:
123 – One Two Three.

index.html

<!DOCTYPE html>

<html>
<body>

<form method=get action="test.jsp">

Enter Any Number : <input type=text name=num><br><br>

<input type=submit value="Display">

</form>

</body>

</html>

test.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<html>

<body>

<font color=red>

<%! inti,n;

String s1;

%>

<% s1= request.getParameter("num");

i=0;

do

n=s1.length();

char ch=s1.charAt(i); switch(ch)

case '0': out.println("Zero ");

break;

case '1': out.println("One "); break;

case '2': out.println("Two "); break;


case '3': out.println("Three "); break;

case '4': out.println("Four "); break;

case '5': out.println("Five ");break;

break; case '6': out.println("Six ");break;

break; case '7': out.println("Seven "); break;

case '8': out.println("Eight "); break;

case '9': out.println("Nine "); break;

i++;

while(i<n);

%>

</font>

</body>

</html>

web.xml

<web-app>

<servlet>

<servlet-name>xyz</servlet-name>

<jsp-file>/test.jsp</jsp-file>

</servlet>

<servlet-mapping>

<servlet-name>xyz</servlet-name>

<url-pattern>/test</url-pattern>

</servlet-mapping>

<welcome-file-list>
<welcome-file>index.html</welcome-file>

</welcome-file-list>

</web-app>

4. Write a JSP program to perform Arithmetic operations such as Addition, Subtraction.


Multiplication and Division. Design a HTML to accept two numbers in text box and radio
buttons to display operations. On submit display result as per the selected operation on
next page using JSP.

index.html

<html>

<title>Arithmatic </title>

<body>

<form method="post" action="ArithmaticOperation.jsp">

<fieldset style="width:30%; background-color:#b3d1ff">

<h2><center> Arithmatic Operation</center></h2>

<hr>

<font size=5 face="Times New Roman">

<input type="radio" name="r1" value="add" checked>Addition</input><br>

<input type="radio" name="r1" value="sub">Subtraction</input><br>

<input type="radio" name="r1" value="mul" >Multiplication</input><br>

<input type="radio" name="r1" value="div" >Division</input><br>

</font>

<table>

<tr>

<td>Enter First No:</td>

<td> <input type="text" name="n1" value=""></td>

</tr>

<tr>

<td>Enter Second No: </td>


<td><input type="text" name="n2" value=""></td>

</tr><br>

<tr>

<td></td>

<td><input type="submit" name="result" value="Submit!"></td>

</tr>

</table>

</fieldset>

</form>

</body>

</html>

ArithmaticOperation.jsp

<html>

<body>

<H1><center>Result for <%=request.getParameter("r1")%></center></H1>

<%

String s1=request.getParameter("n1");

String s2=request.getParameter("n2");

int num1=Integer.parseInt(s1);

int num2=Integer.parseInt(s2);

int op=0;

String str=request.getParameter("r1");

if(str.equals("add"))

op=num1+num2;

if(str.equals("sub"))

op=num1-num2;
if(str.equals("mul"))

op=num1*num2;

if(str.equals("div"))

op=num1/num2;

%>

Result is: <%=op%>

</body>

</html>

You might also like