Solved

Selecting from two drop down boxes.

Posted on 2002-07-22
9
293 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maven project import to eclipse problems 13 102
How to find missing packages when using Netbeans IDE 8.1 ? 19 46
maven j2ee examles 2 49
spring example non maven 4 82
Cloud-based technologies and services will continue to grow in popularity in 2017 thanks to the simple, scalable and cost-effective solutions they deliver. Here are three areas where cloud adoption is poised to really take off.
In 2017, ransomware will become so virulent and widespread that if you aren’t a victim yourself, you will know someone who is.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 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

17 Experts available now in Live!

Get 1:1 Help Now