Solved

Selecting from two drop down boxes.

Posted on 2002-07-22
9
292 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 150 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
 

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Use of TCL script on Cisco devices:  - create file and merge it with running configuration to apply configuration changes
For cloud, the “train has left the station” and in the Microsoft ERP & CRM world, that means the next generation of enterprise software from Microsoft is here: Dynamics 365 is Microsoft’s new integrated business solution that unifies CRM and ERP fun…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now