0 votes
1 view
in Java by (9.2k points)

Below is my code for a simple form to submit. 

@WebServlet("/Main")

public class Main extends HttpServlet {

    private static final long serialVersionUID = 1L;

    /**

     * @see HttpServlet#HttpServlet()

     */

    public Main() {

        super();

        // TODO Auto-generated constructor stub

    }

    public String date;

    public String getDate() {

        return date;

    }

    /**

     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse

     *      response)

     */

    protected void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        // TODO Auto-generated method stub

        // response.getWriter().append("Served at: ").append(request.getContextPath());

        System.out.println("\n-----------------------------------------\nBegin doGet");

        System.out.println("Date " + date);

        HttpSession session = request.getSession();

        // Date - Get today date to fill the welcome form

        Date dNow = new Date();

        SimpleDateFormat ft = new SimpleDateFormat("dd MMMM yyyy");

        date = (String) ft.format(dNow);

        session.setAttribute("date", date);

        // Go to main page

        this.getServletContext().getRequestDispatcher("/WEB-INF/main.jsp").forward(request, response);

        System.out.println("\nEnd doGet\n-----------------------------------------");

    }

    /**

     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse

     *      response)

     */

    protected void doPost(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        System.out.println("\n-----------------------------------------\nBegin Post Session");

        System.out.println("Date " + date);

        HttpSession session = request.getSession();

        this.getServletContext().getRequestDispatcher("/WEB-INF/main.jsp").forward(request, response);

        System.out.println("\nEnd doPost\n-----------------------------------------");

    }

}

I get the correct value in the eclipse console.  But Can anyone tell me how to display the current date in JSP? 

1 Answer

0 votes
by (19k points)

To access session attributes in JSP pages, you can do the below: 

Use ${sessionScope.date} instead of ${date} in <p> tag.

Interested in Java? Check out this Java Certification by Intellipaat.

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...