0% found this document useful (0 votes)
6 views

Java Punishment notes

The document explains key concepts in Java servlets, including ServletContext, URL rewriting, HttpSession, Cookies, and RequestDispatcher. It provides examples of how to implement each concept in code, demonstrating their functionality in web applications. Each section highlights the purpose and usage of these components in managing user sessions and data in a servlet environment.

Uploaded by

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

Java Punishment notes

The document explains key concepts in Java servlets, including ServletContext, URL rewriting, HttpSession, Cookies, and RequestDispatcher. It provides examples of how to implement each concept in code, demonstrating their functionality in web applications. Each section highlights the purpose and usage of these components in managing user sessions and data in a servlet environment.

Uploaded by

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

# 1.Explain ServletContext with web.

xml file
ServletContext is an interface that provides a communication mechanism
between the servlet container and the servlet. It gives the servlet
information about the web application. We can define initialization
parameters and other configurations in the `web.xml` file, which is
accessible through the `ServletContext` object.
web.xml Example:
```xml
<web-app>
<context-param>
<param-name>email</param-name>
<param-value>admin@xyz.com</param-value>
</context-param>
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.example.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/myServlet</url-pattern>
</servlet-mapping>
</web-app>
```
java:
```java
public class MyServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
ServletContext context = getServletContext();
String email = context.getInitParameter("email");
response.getWriter().println("Admin Email: " + email);
}
}
```
# 2.Explain URL rewriting with an example
URL rewriting is a session tracking technique where the session ID is
included in the URL as a request parameter. It is useful when the user
has disabled cookies in the browser.
Example:
```java
public class MyServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
HttpSession session = request.getSession();
session.setAttribute("username", "JohnDoe");
String encodedURL = response.encodeURL("welcome.jsp");
response.sendRedirect(encodedURL); // URL Rewriting will add
session info to the URL
}
}
```
# 3.Explain HttpSession object with an example
HttpSession is an interface that allows a servlet to maintain a session
with an HTTP client across multiple requests. It is used to store
information about the user for the duration of their session.
Example:
```java
public class MyServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
HttpSession session = request.getSession();
session.setAttribute("username", "JohnDoe");
response.getWriter().println("Username stored in session: " +
session.getAttribute("username"));
}
}
```
# 4.Explain Cookies object with an example
Cookies are small pieces of data that are sent from the server and stored
in the user's browser. They are used to track and remember information
about the user's session.
Example:
```java
public class CookieServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
Cookie cookie = new Cookie("username", "JohnDoe");
cookie.setMaxAge(3600); // 1 hour
response.addCookie(cookie);
response.getWriter().println("Cookie set for username.");
}
}
```
# 5.Explain RequestDispatcher Interface with an example
The `RequestDispatcher` interface provides the capability to forward a
request from one servlet to another or to include content from another
resource in the response.
Example:
```java
public class MyServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
String username = request.getParameter("username");
if (username == null || username.isEmpty()) {
RequestDispatcher dispatcher =
request.getRequestDispatcher("error.jsp");
dispatcher.forward(request, response); // Forwarding the
request to error.jsp
} else {
RequestDispatcher dispatcher =
request.getRequestDispatcher("welcome.jsp");
dispatcher.forward(request, response); // Forwarding the
request to welcome.jsp
}
}
}
```

You might also like