(netbeans latest, windows 7)
So, from what I understood, once I start a servlet program
the html page with a form in it loads and I can debug the program only when I click the Submit button
at the end of the form.

if the form is doPost then by using the doPost function.

Now , if I would like to get into the servlet functions instantly when the
html page load ( in order to check for sessions and such)
how can I do it with using java commands?
( ** like the doPost but without the need to click any submit button** )

everything I googled was related to javascript or weird commands in the html that I dont understand

Thanks again,
Despairy

Dani AI

Generated

As correctly described, servlets only run when an HTTP request reaches them. If the goal is to execute server-side checks at the moment the page is requested (instead of waiting for a form POST or using client-side auto-submit), the page must be served by server code — typically a servlet doGet or a JSP — rather than by a static HTML file. ’s auto-submit JavaScript is a valid client-side hack, but the cleaner server-side approach is to have the initial GET hit servlet logic that checks the session and then forwards to the form view.

Example (minimal doGet that checks session and forwards to a JSP):

@WebServlet("/form")
public class FormServlet extends HttpServlet {
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    HttpSession session = req.getSession(false); // do not create a new session
    if (session == null || session.getAttribute("user") == null) {
      resp.sendRedirect(req.getContextPath() + "/login");
      return;
    }
    req.setAttribute("prefill", /* server-side data */);
    req.getRequestDispatcher("/WEB-INF/jsp/form.jsp").forward(req, resp);
  }
}

For site-wide session/login checks, a Filter mapped to relevant URL patterns (for example "/*") is often better: the Filter intercepts requests before servlets and can redirect unauthenticated users to login. Practical tips: map the servlet to the URL the browser requests (avoid serving a separate static HTML), put JSPs under /WEB-INF to force access via servlet, use req.getSession(false) to detect existing sessions, set breakpoints in NetBeans on doGet/doFilter when debugging, and clear browser cache during testing. Auto-submitting forms on load hurts accessibility and can confuse users; prefer server-side checks where possible.

Recommended Answers

All 2 Replies

Servlet technology is based on request / response model.
When user requests something to the server by sending an HTML request.

In the current case user can place a "get" request for the HTML page and server sends the htmlpage which is displayed on the client's browser.

So now after page is loaded on client's side(on browser) it is the client who is having control of the page and server does not know what is happening at client end on the HTML-page.
The server will be notified only after user clicks the submit button on the HTML page
Here client is sending a post/ get request again to the server.

Hence if you want to debug on the HTML page served on client side you need java script.

If you need more details , you can start your applicaiton server in DEBUG mode and verify the server logs for more details.

Well I did manage to do what I needed using the following code:

<SCRIPT LANGUAGE="JavaScript"><!--
setTimeout('document.sessions.submit()',0);
//--></SCRIPT>

But I was wondering if I can do so without javascript and only handling the servlets files.
As you said the control of the page is by the user , is there a way I could give
the control to the servlet and not the user but still the user will be able to submit
stuff?

Thanks for the informative response :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.