Practical 16
make seperate file for each codeRohit143
plain_text
3 years ago
2.2 kB
13
Indexable
Never
<!DOCTYPE html> <html lang="en"> <head> <title>Login</title> </head> <body> <center> <h1>Enter your name</h1> <form action="login" method="get"> Name:<input type="text" name="uname"/><br/> <input type="submit" value="login"/> </form> </center> </body> </html> _________________________________________ import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class Practical16 extends HttpServlet{ public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException{ res.setContentType("text/html"); PrintWriter pw = res.getWriter(); HttpSession session =req.getSession(); String username = req.getParameter("uname"); pw.println("<h1>Welcome ! "+username); pw.println("Session id : "+session.getId()); session.setAttribute("username", username); pw.print("<a href='servlet2'>visit</a>"); pw.close(); } } ____________________________________________ import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class practical16b extends HttpServlet{ public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException{ res.setContentType("text/html"); PrintWriter pw = res.getWriter(); HttpSession session =req.getSession(); String n = (String)session.getAttribute("username"); pw.println("Username : "+n); pw.close(); } } _____________________________________________ <web-app> <servlet> <servlet-name>practical16</servlet-name> <servlet-class>Practical16</servlet-class> </servlet> <servlet-mapping> <servlet-name>practical16</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping> <servlet> <servlet-name>practical16b</servlet-name> <servlet-class>Practical16b</servlet-class> </servlet> <servlet-mapping> <servlet-name>practical16b</servlet-name> <url-pattern>/servlet2</url-pattern> </servlet-mapping> </web-app>