Hello Friends,
I am trying to learn JSP, Servlets at the moment.

What am trying to do : A simple text field and a button
What should happen : When you click the button a text should appear something which is specified in the code. for example, "Pradeep".

I am really finding it difficult at the moment to actually bring the JSp and Servlet in connection.

Can anyone help me??

It would be great to start of with a problem to be solved an dthen move ahead.

Thanks and Rgds,
Pradeep (KINO)

Dani AI

Generated

A short, practical explanation that ties the posts together. is right about the flow: the form posts to a servlet, the servlet supplies the value, and a JSP renders that value. , three small mismatches in the posted code are the likely cause of the empty field.

First, attribute vs parameter: the servlet sets a request attribute, while the JSP reads a request parameter (via request.getParameter). Attributes and parameters are different; when a servlet sets data for a JSP to render, the JSP should read the attribute (or use EL, which checks attributes/scopes automatically). Second, HTML syntax: the value attribute must be quoted — unquoted output can break the markup and make the browser drop the value. Third, forwarding/target: the servlet forwards to whichever JSP should render the form (forward keeps request attributes; a redirect would not).

Minimal examples (use EL in JSP and forward from the servlet):

<input type="text" name="name" value="${name}" />
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    req.setAttribute("name", "Pradeep");
    req.getRequestDispatcher("/view.jsp").forward(req, resp); // view.jsp reads ${name}
}

Checklist for debugging:

  • Confirm the servlet mapping matches the form action (use a context-relative path or <c:url>).
  • Inspect the generated HTML (View Source) to see the value="..." actually rendered.
  • Remove if (request==null) checks — request is never null in a JSP.
  • Use server logs (or System.out.println) to verify the servlet runs and sets the attribute.
  • Prefer EL/JSTL over scriptlets for clarity and to avoid null/quoting surprises.

Following those fixes will make the field show "Pradeep" when the button posts to the servlet and the servlet forwards to the JSP.

Recommended Answers

All 2 Replies

the button (when activated) submits a post request to the servlet, the servlet sets some information in the http requests and forwards to the JSP.

What part of that don't you get working? It's rather basic...

[quote=jwenting;310731]the button (when activated) submits a post request to the servlet, the servlet sets some information in the http requests and forwards to the JSP.

What part of that don't you get working? It's rather basic...[/quote]

Hello Friend,

Index.jsp :

<html>
  <head>
    <title></title>
  </head>
  <body>
    <h2></h2>  
    <form method="post" action="displayname">
      <% String name="";%>
<% if(request==null){
    name="";
}
else{
  name=request.getParameter("name");
}
%>

            Display Name:

            <input type="text" name="name" value=<%=name%> />
<br>
            <input type="submit" name="display" value="Display" />

    </form>
   </body>
</html>

HelloServlet.java

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class HelloServlet extends HttpServlet{

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

        request.setAttribute("name", "Pradeep");
        getServletContext()
     .getRequestDispatcher("/view.jsp").forward(request, response);
    }
    public void doGet(HttpServletRequest  request, 
                      HttpServletResponse response) 
                  throws ServletException, IOException{
        getServletContext()
     .getRequestDispatcher("/view.jsp").forward(request, response);
    }
}

Any wrong in this code??

please help me try to debug.. Thanks a lot!

KINO

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.