[go: up one dir, main page]

0% found this document useful (0 votes)
14 views9 pages

Experiment 22

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 9

Experiment 22

1. Types of Servlets
a. Generic servlets
i. Extend javax.servlet.GenericServlet.
ii. Are protocol independent. They contain no inherent HTTP
support or any other transport protocol.
iii. The default port for the Apache Tomcat service is 8080. This
port is defined for HTML traffic along with the more often
used port 80.
b. HTTP servlets
i. Extend javax.servlet.HttpServlet.
ii. Have built-in HTTP protocol support and are more useful in
a Sun Java System Web Server environment.
iii. The GlassFish Server port number: The default is 8080. The
administration server's port number: The default is 4848.
An administration user name and password: The default
user name is admin , and by default no password is
required.
2. Difference Between doGet() and doPost()
a. doGet() :-
i. Parameters are appended to the URL, and sent along with
the header information. So it brings up a security issues (eg.
Password).
ii. We can send maximum size of data 240 bytes.
iii. Parameters are not encrypted.
iv. Processing is Faster
v. Bookmark is possible
b. doPost() :-
i. Parameters are sent through Request body.
ii. We can send a large amount of data.
iii. Parameters are encrypted.
iv. Processing is Slower
v. Bookmark is not possible.
3. Explain ServletConfig and ServletContext
a. ServletConfig
i. ServletConfig is servlet specific
ii. Parameters of servletConfig are present as name-value pair
in inside .
iii. ServletConfig object is obtained by getServletConfig()
method
iv. Each servlet has got its own ServletConfig object
v. Use ServletConfig when only one servlet needs information
shared by it.
b. ServletContext
i. ServletContext is for whole application
ii. Parameters of servletContext are present as namevalue pair
in which is outside of and inside
iii. ServletContext object is obtained by getServletContext()
method.
iv. ServletContext object is only one and used by different
servlets of the application.
v. Use ServletContext when whole application needs
information shared by it.
4. ServletInputStream and ServletoutputStream
a. ServletInputStream
i. The ServletInputStream class extends java.io.InputStream. It
is implemented by the servlet container and provides an
input stream, that a developer can use to read the data
from a client request.
ii. Methods of ServletInputStream
1. readline() : is used to read the input values.
b. ServletOutputStream
i. ServletOutputStream is an abstract class in the javax.servlet
package. It is used in servlets to write the response to the
client.
ii. ServletOutputStream class extends java.io.OutputStream. It
also defines the print( ) and println( ) methods, which
output data to the stream.
iii. Methods of ServletOutputStream
1. print(boolean) : Prints Boolean value(true/false).
2. print(String) : Prints string values.
3. print(char) : Prints a single character.
4. print(float) : Prints float value( uses 32 bits).
5. print(double) : Prints double value.
6. print(int) : Prints an integer type value.
7. print(long) : Prints long value.
X
1. GetLength.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

@WebServlet("/GetLength")
public class GetLength extends HttpServlet {
private static final long serialVersionUID = 1L;

public GetLength() {
super();
}

protected void doGet(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {

String responseString[] = {
"<link rel='stylesheet'
href='https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css'>",
"<script defer
src='https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js'></
script>" };
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
String s = request.getParameter("username");
for (String str : responseString) {
pw.println(str);
}
pw.println("<div class='container'><h3>The Length of the Username is " +
s.length() + "</h3></div>");
pw.close();

protected void doPost(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
doGet(request, response);
}

}
index.html
<!DOCTYPE html><html><head><meta charset="ISO-8859-1" /><link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css" />
<script defer
src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"
></script> <title>Experiment
22</title></head><style>.container{padding:15px;width:100vw;height:100vh;display:flex;fle
x-direction:column;justify-content:center;align-items:center}.row{width:100%;margin:5px
0px 5px 0px}.col{display:flex;flex-direction:column;justify-content:center;align-
items:center}#username{width:50%;text-align:center}::placeholder{text-
align:center}input[type="submit"]{width:100%;color:white}.btn{width:50%}</
style><body><form action="GetLength" method="GET" class="container"><div
class="row"><div class="col s12"><h4>Enter Username</h4></div></div><div
class="row"><div class="input-field col s12"> <input id="username" name="username"
type="text" class="validate" placeholder="Username" /></div></div><div class="row"><div
class="col s12"> <input type="submit" class="waves-effect waves-light btn"
value="Submit" /></div></div></form></body></html>
2. AuthenticationServlet.java
import java.io.IOException;
<html> <body>
importaction="AthenticationServlet”
<form java.io.PrintWriter;
import javax.servlet.ServletException;
method=”POST”>
import
User Name:<input
javax.servlet.http.HttpServlet;
type="text" name="username"><br>
import javax.servlet.http.HttpServletRequest;
Password:<input type="password" name="password" ><br>
import type="submit">
<input javax.servlet.http.HttpServletResponse;
</form>
public class AthenticationServlet extends HttpServlet {
</body><html>
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String pass = "abhishek12345";
String username, password;
username = request.getParameter("username");
password = request.getParameter("password");
if (username.equals(uname) && password.equals(pass)) {
out.println("Login Successfull");
} else {
out.println("Login Unsuccessfull");
}
}
}

index.html
3. ReturnMarks.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
@WebServlet("/ReturnMarks")
public class ReturnMarks extends HttpServlet {
private static final long serialVersionUID = 1L;
public ReturnMarks() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
String name = request.getParameter("name");
String department = request.getParameter("department");
float percentage = Float.parseFloat(request.getParameter("percentage"));
String year = request.getParameter("year");
pw.println("<link rel='stylesheet'
href='https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css'
integrity='sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr
2' crossorigin='anonymous' /> <script defer src='https://code.jquery.com/jquery-
3.5.1.slim.min.js'
integrity='sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXa
Rkfj' crossorigin='anonymous' ></script> <script defer
src='https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js'
integrity='sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/
tE3MoK7ZeZDyx' crossorigin='anonymous' ></script>
<style>.main{width:100vw;height:100vh;display:flex;justify-content:center;align-
items:center}.card{text-align:center}.row{margin-bottom:5px;margin-top:5px}</style><div
class='container main'><div class='card' style='width: 500px'><div class='card-header'><h4>"
+ name + "</h4></div><div class='card-body'><ul class='list-group list-group-flush'></ul><li
class='list-group-item'><div class='row'><div class='col-6'>Department</div><div class='col-
6'>"+ department + "</div></div></li><li class='list-group-item'><div class='row'><div
class='col-6'>Year</div><div class='col-6'>"+ year + "</div></div></li><li class='list-group-
item'><div class='row'><div class='col-6'>Percentage</div><div class='col-6'>"+ percentage
+"</div></div></li><li class='list-group-item'><div class='row'><div
class='col-6'>Status</div><div class='col-6'>"+ (percentage > 35 ? "Passed" : "Failed") +
"</div></div></li></ul></div></div></div>");
pw.close();

}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
index.html
<!DOCTYPE html><html><head><meta charset="ISO-8859-1" /><link rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css"
integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xX
r2" crossorigin="anonymous" /> <script defer src="https://code.jquery.com/jquery-
3.5.1.slim.min.js"
integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCX
aRkfj" crossorigin="anonymous" ></script> <script defer
src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/
tE3MoK7ZeZDyx" crossorigin="anonymous" ></script> <title>Experiment
22</title></head><style>.container{padding:15px;width:100vw;height:100vh;display:flex;fle
x-direction:column;justify-content:center;align-items:center}.form-
group{width:100%}.check{display:flex;flex-direction:row;justify-content:center;align-
items:center}</style><body><form action="ReturnMarks" method="GET"
class="container"><div class="form-group"> <label for="name">Name</label> <input
type="text" class="form-control" id="name" placeholder="Enter Name" name="name"
/></div><div class="form-group"> <label for="percentage">Percentage</label> <input
type="text" class="form-control" id="percentage" placeholder="Enter Percentage"
name="percentage" /></div><div class="form-group"> <label
for="department">Department</label> <select class="form-control" id="department"
name="department"><option value="Information Technology">Information
Technology</option><option value="Computer Engineering">Computer
Engineering</option><option value="Civil Engineering">Civil Engineering</option><option
value="Mechanical Engineering">Mechanical Engineering</option> </select></div><div
class="form-group check"><div class="form-check form-check-inline"> <input class="form-
check-input" type="radio" name="year" id="FY" value="First Year" checked /> <label
class="form-check-label" for="FY"> FY </label></div><div class="form-check form-check-
inline"> <input class="form-check-input" type="radio" name="year" id="SY" value="Second
Year" /> <label class="form-check-label" for="SY"> SY </label></div><div class="form-check
form-check-inline"> <input class="form-check-input" type="radio" name="year" id="TY"
value="Third Year" /> <label class="form-check-label" for="TY"> TY </label></div></div><div
class="form-group"> <button type="submit" class="btn btn-primary
btn-block">Submit</button></div></form></body></html>

You might also like