?
Solved

Selecting from two drop down boxes.

Posted on 2002-07-22
9
Medium Priority
?
302 Views
Last Modified: 2010-04-01
I have a jsp page and it has two drop down boxes. If choose Category1 from the first and Subcategory1 from the second drop down and click OK button it should take me to subcategory1.jsp. If i choose Category1 and Subcategory2 from the second drop down and click OK button it should take me to Subcategory2.jsp. If i choose Catgeory1 and SubCategory3 it should send me to error page. How do i do it in Jsp without using Javascript. I would appreciate a code for this.

 Ramani


<html>
<body>
<form NAME="TControlForm">
<table  border="0" cellspacing="0" cellpadding="0">
    <tr>
       <td >View </td>
             
         <td><select name="TType" size="3" multiple >
<option selected></option>
<option>Category1</option>
<option>Category2</option>
<option>Category3</option>
<option>Category4</option>
<option>Category5</option></select><br></td>
                     <td><select name="TCategory" size="3" multiple >
                     <option selected>All</option>
<option>SubCategory1</option>
<option>SubCategory2</option>
<option>SubCategory3</option>
<option>SubCategory4</option>
<option>---------------------------</option></select><br></td>
   </tr>
</table>
</body>
</html>
0
Comment
Question by:ramani081399
  • 4
  • 3
  • 2
9 Comments
 
LVL 19

Expert Comment

by:cheekycj
ID: 7170430
one option could be to have the JSP submit to a redirect JSP that reads in the form submitted fields and based on the values redirects the user accordingly.

CJ
0
 

Author Comment

by:ramani081399
ID: 7170456
cheekycj,
I am looking for code to acomplish this.
Ramani
0
 
LVL 6

Accepted Solution

by:
kotan earned 600 total points
ID: 7170932
trigger below servlet when click OK button.


void doPost(HttpServletRequest req, HttpServletResponse resp)
{
    String TType = req.getParameter( "TType" );
    String TCategory = req.getParameters( "TCategory" );
   
    if ( "Category1".equals( TType ) )
    {
        if ( "SubCategory1".equals( TCategory ) )
        {
            resp.sendRedirect( "subcategory1.jsp" );
        }
        else if ( "SubCategory2".equals( TCategory ) )
        {
            resp.sendRedirect( "subcategory2.jsp" );
        }
        else if ( "SubCategory3".equals( TCategory ) )
        {
            resp.sendRedirect( "error.jsp" );
        }
    }
}
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:ramani081399
ID: 7173016
Kotan,

  I tried the way u have sugessted but get a error saying that forwarding URL cannot be found.
package FirstJSP;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class TestDropDown extends HttpServlet {
 
 
   String defaultName;
   String TType ;
   String TCategory;

 
  public void init(ServletConfig config) throws ServletException
  {
    super.init(config);
   }
 
  public void doPost(HttpServletRequest req, HttpServletResponse res)
       throws IOException
  {
    // Must set the content type first
    res.setContentType("text/html");
    // Now we can obtain a PrintWriter
    PrintWriter out = res.getWriter();
   
    defaultName = req.getParameter("yourname");
    out.println("<h4>");
    out.println("Hello" + " " + defaultName + "!");
    out.println("</h4>");
     TType = req.getParameter( "TType" );
     TCategory = req.getParameter( "TCategory" );

     if ( "Category1".equals( TType ) )
   {
       if ( "SubCategory1".equals( TCategory ) )
       {
           res.sendRedirect( "SubCategory1.jsp" );
       }
       else if ( "SubCategory2".equals( TCategory ) )
       {
           res.sendRedirect( "SubCategory2.jsp" );
       }
       else if ( "SubCategory3".equals( TCategory ) )
       {
           res.sendRedirect( "error.jsp" );
       }
   }
}

     
 
}

Form:

<html>
<body>
<form NAME="TControlForm" action="TestDropDown" method="post">
Enter your name:

<input type="text" name="yourname" size = 14>
<table  border="0" cellspacing="0" cellpadding="0">
   <tr>
      <td >View </td>
             
        <td><select name="TType" size="3" multiple >
<option selected></option>
<option>Category1</option>
<option>Category2</option>
<option>Category3</option>
<option>Category4</option>
<option>Category5</option></select><br></td>
                    <td><select name="TCategory" size="3" multiple >
                    <option selected>All</option>
<option>SubCategory1</option>
<option>SubCategory2</option>
<option>SubCategory3</option>
<option>SubCategory4</option>
<option>---------------------------</option></select><br></td>
  </tr>

</table>
<br>

<input type="submit" name="submit" value="Go">
</body>
</html>
0
 
LVL 6

Expert Comment

by:kotan
ID: 7173250
try

res.sendRedirect( "/SubCategory1.jsp" );
0
 

Author Comment

by:ramani081399
ID: 7174297
Kotan,
No it does not work. I have put the servlet testDropDown.java in package called FirstJSP. The compiled class i have put it under Web-Inf/classes/FirstJSP/TestDropDown.class
I get the error
Error 404--Not Found
From RFC 2068 Hypertext Transfer Protocol -- HTTP/1.1:
10.4.5 404 Not Found
The server has not found anything matching the Request-URI
 
Ramani
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 7174369
How are you forwarding and/or accessing the servlet/JSPs?

Do you have entries in your web.xml to map URIs to Servlets?

CJ
0
 

Author Comment

by:ramani081399
ID: 7175107
cheekycj
no i dont have entries in web.xml.
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 7175193
you need to map your URIs to your servlets.

Like:

one entry in your web.xml would be:

    <servlet-mapping url-pattern="TestDropDown.jsp" servlet-name="TestDropDown" />
         <servlet>
            <servlet-name>TestDropDown</servlet-name>
            <servlet-class>FirstJSP.TestDropDown</servlet-class>
            <load-on-startup/>
        </servlet>

Now in your form update it to be:
<form NAME="TControlForm" action="TestDropDown.jsp" method="post">

Now if each SubCategory*.jsp is a servlet not a jsp you will have to add mapping such as the one above in the web.xml for each one of those.

CJ
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the below post we have mentioned the best hosting type for startups. Also, check out some of the superlative web hosting companies that are proposing affordable web hosting solutions to host your startup website.
Social messanging services like WhatsApp and Facebook can help businesses in ways that many owners don't even imagine, giving new opportunities to connect with customers. Discover some of the most innovative things they can do for your company.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question