Solved

Selecting from two drop down boxes.

Posted on 2002-07-22
9
294 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
library class in java 1 92
maven j2ee examles 2 55
timeToIdleSeconds vs timeToLiveSeconds in hibernate second level cache 3 124
login form jsp example 2 25
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This problem is more common than not and I will show you some things to check to solve this problem.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

773 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