You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Edilmar Alves <ed...@intersite.com.br> on 2000/12/20 21:09:39 UTC

Doubts joining JSP x Servlets

Hi,

I'm new into JSP technology.
I have used only Servlets before,
with JBuilder 3 Enterprise.

I would like to know a system with
JSP+Servlets to make something like
these steps I have into my servlet:

1) I have a HTML <form> with a text
   field. The user types a username
   that should be searched into a
   database.

2) JSP receives a request and call
   a servlet to verify the username.
   DOUBT: How to start the communication
          between JSP and Servlet and
          how to send username data ?

3) Servlet searches for username into a
   database. If there is, it should return
   all records matching this name with the
   fields Name/Address/etc; otherwise, it
   should return an error msg.
   DOUBT: Is it possible to return a Query
          result object from Servlet to JSP?
          How to return error msg ?

4) JSP should receive the Query (or error msg)
   and format the HTML output to the browser.

At my servlets, many times I need to do something
like this:
if ("some code condition")
  out.println("<font size=X1 color=Y1 ...");
else if ("some other code condition")
  out.println("<font size=X2 color=Y2 ...");
...
else
  out.println("<font size=Xn color=Yn ...");
This is a simple example of HTML code that is
linked to logic conditions. I think this type
of code has to stay at Servlet, not JSP,isn't it?



Re: Doubts joining JSP x Servlets

Posted by Matt Goss <mg...@rtci.com>.
Edimar,
What you need to do is have a form on an html page that posts to the
servlet. handle the database access and data validation there, then and
add the return data to the request, or the session and then forward it
to the proper jsp page for display. (if the object is added to the
session the bean scope is session, if request then the scope is request)
for instance
<code sample>
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
    {
        String username = req.getParameter("USERNAME");
        String password = req.getParemeter("PASSWORD");
        //do your dta validation, database access, and data processing
then..
        ImaginaryUserObject IUO = new
ImaginaryUserObject(thereturndata);
        req.setAttribute("theIUO", IUO);
        RequestDispatcher rd =
req.getRequestDispatcher("/displaypage.jsp");
        rd.forward(req, resp);
        }
</code sample>
then in your jsp get an instance of the object and display the data
<jsp sample>
<jsp:useBean id="theIUO" class="ImaginaryUserObject" scope="request" />
<html><body>
the data is <jsp:getProperty name="theIUO" property="thepropertyname"
/><br>
</body></html>
</jsp sample>

hope this helps,
Matt Goss

Edilmar Alves wrote:

> Hi,
>
> I'm new into JSP technology.
> I have used only Servlets before,
> with JBuilder 3 Enterprise.
>
> I would like to know a system with
> JSP+Servlets to make something like
> these steps I have into my servlet:
>
> 1) I have a HTML <form> with a text
>    field. The user types a username
>    that should be searched into a
>    database.
>
> 2) JSP receives a request and call
>    a servlet to verify the username.
>    DOUBT: How to start the communication
>           between JSP and Servlet and
>           how to send username data ?
>
> 3) Servlet searches for username into a
>    database. If there is, it should return
>    all records matching this name with the
>    fields Name/Address/etc; otherwise, it
>    should return an error msg.
>    DOUBT: Is it possible to return a Query
>           result object from Servlet to JSP?
>           How to return error msg ?
>
> 4) JSP should receive the Query (or error msg)
>    and format the HTML output to the browser.
>
> At my servlets, many times I need to do something
> like this:
> if ("some code condition")
>   out.println("<font size=X1 color=Y1 ...");
> else if ("some other code condition")
>   out.println("<font size=X2 color=Y2 ...");
> ...
> else
>   out.println("<font size=Xn color=Yn ...");
> This is a simple example of HTML code that is
> linked to logic conditions. I think this type
> of code has to stay at Servlet, not JSP,isn't it?

RE: Doubts joining JSP x Servlets

Posted by Dave Newton <da...@solaraccess.com>.
> I would like to know a system with
> JSP+Servlets to make something like
> these steps I have into my servlet:

A JSP is a Servlet, so throwing things back and forth
between them is pretty straightforward.

> I think this type of code has to stay at Servlet, not JSP,isn't it?

Well, in a JSP the code would look exactly like HTML, since it just
turns non-script code into prints.

Dave