Session tracking using url rewriting in servlet

For Explanation watch the video

web.xml

<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns=”http://xmlns.jcp.org/xml/ns/javaee” xsi:schemaLocation=”http://xmlns.jcp.org/xml/ns/ja…


This content originally appeared on DEV Community and was authored by NJ

For Explanation watch the video

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>URLRewriting</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<style type="text/css">
div {
    width: 500px;
    margin: auto;
    margin-top: 100px;
}
</style>
</head>
<body>
    <div>
        <form action="firsturl" method="post">
            Name: <br> <input type="text" name="name"> <br>
            Add : <br> <input type="text" name="add"> <br>
            Choose any one: <br> <select name="dish">
                <option value="sweet">Sweet</option>
                <option value="spicy">Spicy</option>
            </select> <br> <input type="submit" value="submit">
        </form>
    </div>
</body>
</html>

FirstServlet.java


package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/firsturl")
public class FirstServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        // get PrintWriter
        PrintWriter pw = res.getWriter();

        // set content type
        res.setContentType("text/html");

        // get the values
        String name = req.getParameter("name");
        String add = req.getParameter("add");
        String dish = req.getParameter("dish");

        if (dish.equalsIgnoreCase("sweet")) {
            pw.println("<h1>Choose One</h1>");
            pw.println("<form action='secondurl?name=" + name + "&add=" + add + "' method='post'>");
            pw.println("<select name='dish'>");
            pw.println("<option value='gulab jamun'>gulab jamun</option>");
            pw.println("<option value='jalebi'>jalebi</option>");
            pw.println("</select>");
            pw.println("<br>");
            pw.println("<input type='submit' value='submit'>");
            pw.println("</form>");
        } else {
            pw.println("<h1>Choose One</h1>");
            pw.println("<form action='secondurl?name=" + name + "&add=" + add + "' method='post'>");
            pw.println("<select name='dish'>");
            pw.println("<option value='samosa'>samosa</option>");
            pw.println("<option value='paneer'>paneer</option>");
            pw.println("</select>");
            pw.println("<br>");
            pw.println("<input type='submit' value='submit'>");
            pw.println("</form>");
        }
        // close the stream
        pw.close();
    }
}


SecondServlet.java

package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/secondurl")
public class SecondServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        // get PrintWriter
        PrintWriter pw = res.getWriter();

        // set content type
        res.setContentType("text/html");

        // get the values
        String name = req.getParameter("name");
        String dish = req.getParameter("dish");
        String add = req.getParameter("add");

        pw.println("<h2>Thanks you " + name + " your " + dish + " will be placed at " + add + "</h2>");
        // close the Stream
        pw.close();
    }
}




This content originally appeared on DEV Community and was authored by NJ


Print Share Comment Cite Upload Translate Updates
APA

NJ | Sciencx (2023-05-07T03:02:09+00:00) Session tracking using url rewriting in servlet. Retrieved from https://www.scien.cx/2023/05/07/session-tracking-using-url-rewriting-in-servlet/

MLA
" » Session tracking using url rewriting in servlet." NJ | Sciencx - Sunday May 7, 2023, https://www.scien.cx/2023/05/07/session-tracking-using-url-rewriting-in-servlet/
HARVARD
NJ | Sciencx Sunday May 7, 2023 » Session tracking using url rewriting in servlet., viewed ,<https://www.scien.cx/2023/05/07/session-tracking-using-url-rewriting-in-servlet/>
VANCOUVER
NJ | Sciencx - » Session tracking using url rewriting in servlet. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/05/07/session-tracking-using-url-rewriting-in-servlet/
CHICAGO
" » Session tracking using url rewriting in servlet." NJ | Sciencx - Accessed . https://www.scien.cx/2023/05/07/session-tracking-using-url-rewriting-in-servlet/
IEEE
" » Session tracking using url rewriting in servlet." NJ | Sciencx [Online]. Available: https://www.scien.cx/2023/05/07/session-tracking-using-url-rewriting-in-servlet/. [Accessed: ]
rf:citation
» Session tracking using url rewriting in servlet | NJ | Sciencx | https://www.scien.cx/2023/05/07/session-tracking-using-url-rewriting-in-servlet/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.