-3

I want to load a Courses page in my project with all the courses name in the database.

Here we can see an example edx courses page

The data about all the courses is loaded without any button click or from submission. So how can I do the same?

Here is what I tried for an simple example by sending a string to client1.jsp from Server1.java. But When I open client1.jsp it shows nothing .

File-Servlet1.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import javax.websocket.Session;

import java.util.*;


@WebServlet("/Servlet1")
public class Servlet1 extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletRequest request, HttpServletResponse 
        response) throws ServletException, IOException {

        String name="Rahul";
        HttpSession s=request.getSession(true);

        s.setAttribute("myname", name);
        response.sendRedirect("client1.jsp");


    }

}

File-client1.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title>Insert title here</title>
        </head>
    <body>
        <div><h1>${myname}</h1></div>
        <h1><%session.getAttribute("myname");%></h1>

    </body>
</html>
Morteza Jalambadani
  • 2,190
  • 6
  • 21
  • 35
Rahul Choubey
  • 29
  • 1
  • 10

1 Answers1

1
  1. You invoke the servlet
  2. the servlet's doGet() method gets the names from the database (as a List<String>, for example) and stores them in an attribute (named names for example) of the request (not the session)
  3. the servlet forwards (not redirects), using the request dispatcher, to the JSP
  4. The JSP, using the JSTL (<c:forEach>) (and not using scriptlets) loops through the names stored in the names attribute. For each name, it displays it using <c:out>.
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • But how it can load the data like on edx website ? I tried yesterday everything you said . – Rahul Choubey Aug 11 '18 at 06:56
  • 1
    By querying your database. I have no idea of what kind of database you're using. If it's a relational database, use [JDBC](h. If ttps://docs.oracle.com/javase/tutorial/jdbc/basics/index.html) or JPA. – JB Nizet Aug 11 '18 at 06:59
  • I can retrieve and easily print it on console by Servlet1.java. But how a client1 can request the data from Servlet1 as I click Courses tab(courses.jsp) – Rahul Choubey Aug 11 '18 at 07:02
  • Read my answer. It's explained. You click the tab. That sends a request to your servlet (step 1), the servlet gets the data from the database (step 2), etc. etc. Of course the URL of the link of the Courses tab must be the URL of the servlet (is that what you're asking?) – JB Nizet Aug 11 '18 at 07:06
  • Thanks but this is what I was looking for -https://stackoverflow.com/questions/3590961/calling-a-servlet-from-jsp-file-on-page-load – Rahul Choubey Aug 11 '18 at 07:49
  • Have you noticed that the code in that answer does exactly what my answer tells you to do? – JB Nizet Aug 11 '18 at 08:18
  • Haha Oh Yes ! thanks again . Actually you saved me another time :) – Rahul Choubey Aug 11 '18 at 08:36