*File Name
1)Web.xml : Welcome file of Servlet.
2) index.html: Which is indicate other File With link.
3)login.html:For Login Name & login Password.
4)logingServlet:For Create Session.
5)logoutServlet:for Loutout Session.
6)ProfileServlet:See Session ProfileView.
*Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>loginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>logoutServlet</servlet-name>
<servlet-class>logoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>logoutServlet</servlet-name>
<url-pattern>/logoutServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ProfileServlet</servlet-name>
<servlet-class>ProfileServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ProfileServlet</servlet-name>
<url-pattern>/ProfileServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
*index.html
<html>
<body>
<a href="login.html">Login</a>||
<a href="logoutServlet">Logout</a>||
<a href="ProfileServlet">Profile</a>
</body>
</html>
*login.html
<html>
<body>
<form action="loginServlet" method="post">
Name:<input type="text" name="name"><br>
password:<input type="password" name="pas"><br>
<input type="submit">
</form>
</body>
</html>
*loginServlet
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class loginServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out=res.getWriter();
String Name=req.getParameter("name");
String password=req.getParameter("pas");
req.getRequestDispatcher("index.html").include(req, res);
if(password.equals("ujash"))
{
HttpSession session=req.getSession();
session.setAttribute("Name", Name);
out.println("<br>Welcome :"+Name);
}
else
{
out.println("You Have entered Wrong Password");
}
}
}
*logoutServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class logoutServlet extends HttpServlet {
public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException, ServletException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
req.getRequestDispatcher("login.html").include(req, res);
HttpSession session=req.getSession();
session.invalidate();
out.println("<br>You have successfully Logout");
}
}
*ProfileServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ProfileServlet extends HttpServlet {
public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException, ServletException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
req.getRequestDispatcher("index.html").include(req, res);
HttpSession session=req.getSession(false);
if(session!=null)
{
String Name=(String) session.getAttribute("Name");
out.println("<br> Welcome Again :" +Name);
}
else
{
req.getRequestDispatcher("index.html").include(req, res);
out.println("<br> Plz Login First");
req.getRequestDispatcher("login.html").include(req, res);
}
}
}
*outPut
Happy Coding
Thanks ;)
1)Web.xml : Welcome file of Servlet.
2) index.html: Which is indicate other File With link.
3)login.html:For Login Name & login Password.
4)logingServlet:For Create Session.
5)logoutServlet:for Loutout Session.
6)ProfileServlet:See Session ProfileView.
*Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>loginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>logoutServlet</servlet-name>
<servlet-class>logoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>logoutServlet</servlet-name>
<url-pattern>/logoutServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ProfileServlet</servlet-name>
<servlet-class>ProfileServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ProfileServlet</servlet-name>
<url-pattern>/ProfileServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
*index.html
<html>
<body>
<a href="login.html">Login</a>||
<a href="logoutServlet">Logout</a>||
<a href="ProfileServlet">Profile</a>
</body>
</html>
*login.html
<html>
<body>
<form action="loginServlet" method="post">
Name:<input type="text" name="name"><br>
password:<input type="password" name="pas"><br>
<input type="submit">
</form>
</body>
</html>
*loginServlet
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class loginServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out=res.getWriter();
String Name=req.getParameter("name");
String password=req.getParameter("pas");
req.getRequestDispatcher("index.html").include(req, res);
if(password.equals("ujash"))
{
HttpSession session=req.getSession();
session.setAttribute("Name", Name);
out.println("<br>Welcome :"+Name);
}
else
{
out.println("You Have entered Wrong Password");
}
}
}
*logoutServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class logoutServlet extends HttpServlet {
public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException, ServletException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
req.getRequestDispatcher("login.html").include(req, res);
HttpSession session=req.getSession();
session.invalidate();
out.println("<br>You have successfully Logout");
}
}
*ProfileServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ProfileServlet extends HttpServlet {
public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException, ServletException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
req.getRequestDispatcher("index.html").include(req, res);
HttpSession session=req.getSession(false);
if(session!=null)
{
String Name=(String) session.getAttribute("Name");
out.println("<br> Welcome Again :" +Name);
}
else
{
req.getRequestDispatcher("index.html").include(req, res);
out.println("<br> Plz Login First");
req.getRequestDispatcher("login.html").include(req, res);
}
}
}
*outPut
Happy Coding
Thanks ;)
Advanced java training in Noida
ReplyDeleteNice
ReplyDeletePhanom Professionals Company in Bangalore provides you with a variety of JAVA software development services for the improvement of your website. We offer competitively cost services for integrating Java, developing Java websites, creating Java programs, creating Java mobile applications, migrating Java, and creating Java content management systems. Software Development Company in Bangalore is always available for you.
ReplyDelete
ReplyDeleteThank you for sharing this valuable content. I genuinely appreciate well-crafted and insightful information like this. The ideas presented here are not only excellent but also incredibly engaging, which enhances the overall reading experience. Keep up the fantastic work, and I look forward to more of your contributions.