Adv Java #2 - Servlets Introduction: - by Raghu Sir (Sathya Technologies, Ameerpet, Hyderabad)
Adv Java #2 - Servlets Introduction: - by Raghu Sir (Sathya Technologies, Ameerpet, Hyderabad)
Adv Java #2 - Servlets Introduction: - by Raghu Sir (Sathya Technologies, Ameerpet, Hyderabad)
1|Page
- by RAGHU SIR [ SATHYA TECHNOLOGIES, AMEERPET, HYDERABAD]
ServletResponse
ServletConfig
ServletContext
Filter
FilterChain
FilterConfig
RequestDispatcher
b.Classes:
GenericServlet
ServletContextEvent
ServletOutputStream
c.Exceptions:
ServletException
UnavailableException
package#2 : javax.servlet.http
a.Interfaces:
HttpServletRequest
HttpServletResponse
HttpSession
HttpSessionListener
b.Class:
Cookie
HttpServlet
package#3 : javax.servlet.annotation
a.Annotations
@WebServlet
@WebInitParam
@WebFilter
@WebListener
23. How many ways we can create dynamic web page using servlets?
By using Servlets API we can create web pages in 3 Ways.
Those are
a. By implementing ‘Servlet’ interface (javax.servlet package)
b. By extending ‘GenericServlet’ abstract class (javax.servlet package)
c. By extending ‘HttpServlet’ abstract class (javax.servlet.http package)
24. What is the default output of a servlet?
25. What is Deployment descriptor?
Web.xml files is called as Deployment Descriptor.
26. Why web.xml ?
This is input file to Web server. It contains Servlet class name, object name, URL,
..etc. Simple web.xml format looks like
<web-app>
<servlet>
<servlet-name> anyName </servlet-name>
<servlet-class> FullClassName </servlet-class>
</servlet>
2|Page
- by RAGHU SIR [ SATHYA TECHNOLOGIES, AMEERPET, HYDERABAD]
<servlet-mapping>
<servlet-name> anyName </servlet-name>
<url-pattern> URL of servlet </url-pattern>
</servlet-mapping>
</web-app>
27. Explain <servlet> tag in web.xml?
28. Explain <servlet-mapping> tag in web.xml?
29. Can we write servlets without using web.xml?
Yes, Possible. Use @WebServlet(“/URL”)
30. Why servlet should have URL? Who can access this?
Browser (client) cannot understand Java code (like Class, object Name..etc)
So, browser can access/read Servlet using its URL.
31. How many life cycle methods in Servlets ?
There are 3 Life Cycle methods in Servlet which are called by Server.
a. init (..) called only once after creating object
b. service(..) called for every request
c. destroy() called only once before destroying object
32. What is the role/work of Web Server in java/servlets ?
A server will,
a. Load servlet class
b. Creates object using default constructor
c. Calls init(..) one time
d. Calls service(..) for every request
e. Calls destroy() at the end of server (before stopping server)
33. Which life cycle methods are called only once?
34. Which life cycle method is called for every request?
35. What is the difference between System.out.println and out.println() of PrintWriter?
System.out.println() prints message on console
Out.println() of PrintWriter prints message one web page (browser)
3|Page
- by RAGHU SIR [ SATHYA TECHNOLOGIES, AMEERPET, HYDERABAD]
4|Page
- by RAGHU SIR [ SATHYA TECHNOLOGIES, AMEERPET, HYDERABAD]
out.println("<html>");
out.println("<head> </head>");
out.println("<body>");
int sub1=90; int sub2=80; int sub3=70;
int total=sub1+sub2+sub3;
out.println("<b>Raghu Total Marks : " +total +" </b>");
5|Page
- by RAGHU SIR [ SATHYA TECHNOLOGIES, AMEERPET, HYDERABAD]
double avg=total/3;
out.println("<u>Raghu Average is :" + avg +"</u>") ;
out.println("</body>");
out.println("</html>");
}
}
4. BillServlet.java
package org.sathyatech;
//ctrl+shift+O
public class BillServlet extends GenericServlet{
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
PrintWriter out=res.getWriter();
out.println("<html>");
out.println("<head> </head>");
out.println("<body>");
out.println("</body>");
out.println("</html>");
}
}
5. ScrollMessageServlet.java
package org.sathayetch;
//ctrl+shift+O
public class ScrollMessageServlet extends GenericServlet{
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
PrintWriter out=res.getWriter();
out.println("<html>");
out.println("<head> </head>");
out.println("<body>");
6|Page
- by RAGHU SIR [ SATHYA TECHNOLOGIES, AMEERPET, HYDERABAD]
}
}
6. ColorCodeServlet.java
package com.app;
//ctrl+shift+O
public class ColorCodeServlet extends GenericServlet{
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
PrintWriter out=res.getWriter();
out.println("<html>");
out.println("<head> </head>");
out.println("<body>");
String color="BLUE";
switch (color) {
case "RED": out.println("REG IS BLOOD"); break;
case "GREEN": out.println("GREEN IS FOOD"); break;
case "WHITE": out.println("WHITE IS PEACE"); break;
default: out.println("CHOOSE ONE COLOR"); break;
}
out.println("</body>");
out.println("</html>");
}
}
7. DateServlet.java
package org.sathyatech;
//ctrl+shift+O
public class DateServlet extends GenericServlet{
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
PrintWriter out=res.getWriter();
out.println("<html>");
out.println("<head> </head>");
out.println("<body>");
java.util.Date dte=new java.util.Date();
System.out.println(dte);
out.println("<p> Now, Date and Time is: " + dte + "</p>");
out.println("</body>");
out.println("</html>");
}
}
7|Page
- by RAGHU SIR [ SATHYA TECHNOLOGIES, AMEERPET, HYDERABAD]
8. RandomNumberServlet.java
package com.app;
//ctrl+shift+O
public class RandomNumberServlet extends GenericServlet{
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
PrintWriter out=res.getWriter();
out.println("<html>");
out.println("<head> </head>");
out.println("<body>");
java.util.Random random = new java.util.Random();
random.nextInt(10);
out.println("</h3> Random Number is : " + random + "</h3>");
out.println("</body>");
out.println("</html>");
}
}
9. StringOperationsServlet.java
package org.sathyatech;
//ctrl+shift+O
public class StringOperationsServlet extends GenericServlet{
@Override
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
PrintWriter out=res.getWriter();
out.println("<html>");
out.println("<head> </head>");
out.println("<body>");
out.println("</body>");
out.println("</html>");
}
}
8|Page
- by RAGHU SIR [ SATHYA TECHNOLOGIES, AMEERPET, HYDERABAD]
10. ResultServlet.java
package com.app;
//ctrl+shift+O
public class SampleServlet extends GenericServlet{
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
PrintWriter out=res.getWriter();
out.println("<html>");
out.println("<head> </head>");
out.println("<body>");
int marks[] = {85,65,98,68,90};
int total =0;
for (int i = 0; i < marks.length; i++) {
total = total + marks[i];
}
out.println("<p> Total marks :" + total +"</p>");
double avg = total / marks.length;
out.println("<p> Average is :" + avg +"</p>");
out.println("</body>");
out.println("</html>");
}
}
4. Write a servlet “DatePage” using URL “/dates’ that prints the current system date
and time using <b> and <marquee> tag.
5. Write a servlet “SathyaPage” using URL ‘/sathya’ that prints “WELCOME TO RAGHU
SIR CLASS” 10 times.
6. Write a servlet “CalculationPage” using URL ‘/calculate’ with below instructions
Create two int variables x,y with values. Print Addition, subtraction, division and
multiplication result in webpage.
7. Write a servlet ‘StudentDataPage’ using URL ‘/students’ that prints HTML table in
below format
9|Page
- by RAGHU SIR [ SATHYA TECHNOLOGIES, AMEERPET, HYDERABAD]
2 KANNA Spring
8. Write a sevlet ‘EmployeePage’ using URL ‘/employee’ that prints HTML Order List
Welcome to Employees
I. RAM (BA)
II. SAM (DEV)
III. KHAN (QA)
IV. AJAY (BA)
V. RAGHU(TL)
9. Write a Servlet “FriendsPage” using URL ‘/friends’ that prints HTML Unordered List
Welcome to Friends Page
• Ajay Kumar
• RAGHU SA
• SHIVA
• VANI
• SUBBU
• KANNA
10. Write a Servlet “RegisterPage” using URL “/register” that prints HTML Form like
Student ID (Text Input)
Student Name (Text Input)
Gender (Radio buttons)
Course (DropDown with values Core Java, Adv Java, Spring, Boot)
Address (TextArea)
Timings interested (checkbox [ ] Morning [ ] Afternoon [ ] Evening )
Submit Button with Text ‘Place Enquiry’
11. Write a Servlet ‘LoginPage’ using URL ‘/login’ that prints HTML login page like
Welcome to Login Page
User Name : Text Input
Password : Password input
Login (submit Button)
FB: https://www.facebook.com/groups/thejavatemple/
EMAIL : [email protected]
10 | P a g e