Ajp Exp 21 Op

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

X.

Program Code:
1. Write a Program to display following output in browser Window.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Hello World!</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Welcome to Maharashtra State Board Of Technical Education!</h1>");
out.println("</body>");
out.println("</html>");
}
}
2. Write the output of following Code.
<html>
<body>
<center>
<form name="Form1" method="post"
action="http://localhost:8080/examples/servlets/servlet/PostParametersServlet">
<table>
<tr>
<td><B>Employee</td>
<td><input type=textbox name="e" size="25" value=""></td>
</tr>
<tr>
<td><B>Phone</td>
<td><input type=textbox name="p" size="25" value=""></td>
</tr>
</table>
<input type=submit value="Submit">
<br><br>
<h4>Executed By 43 Shresth and 58 Smith</h4>
</body>
</html>

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class PostParametersServlet extends GenericServlet {


public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
// Set the content type of the response
response.setContentType("text/html");
PrintWriter pw = response.getWriter();

// Get enumeration of parameter names


Enumeration<String> e = request.getParameterNames();

// Display parameter names and values


while (e.hasMoreElements()) {
String pname = e.nextElement();
String pvalue = request.getParameter(pname);
pw.println(pname + " = " + pvalue + "<br>");
}
pw.close();
}
}
XIII. Exercise:
1. Develop Servlet program to print Hello Msbte in browser Window.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Hello World!</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hello MSBTE</h1>");
out.println("</body>");
out.println("</html>");
}
}

2. Develop a program to receive parameter through HTML forms and send back received
parameter to browser.
<!DOCTYPE html>
<html>
<head>
<title>Parameter Form</title>
</head>
<body>
<h1>Submit Your Details</h1>
<form action="http://localhost:8080/examples/servlets/servlet/ReceiveParametersServlet" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required><br><br>
<label for="age">Age:</label>
<input type="number" id="age" name="age" required><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ReceiveParametersServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String name = request.getParameter("name");
String age = request.getParameter("age");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Received Parameters</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Received Parameters:</h1>");
out.println("<p>Name: " + name + "</p>");
out.println("<p>Age: " + age + "</p>");
out.println("</body>");
out.println("</html>");
}
}

You might also like