Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple forms in a single JSP page

 
Ram Kas
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I have multiple forms in a single JSP Page?

My code is as follows:



But, it doesn't take me to Cart.jsp page.

Any inputs will be highly appreciated.


Thanks

Dinakar Kasturi.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does it do?


By the way: Why are you using out.print statements to build up your HTML?
The point of JSP is prevent the need for such hard to read code.
 
Ram Kas
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It gets items from Products Table and displays it.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand what you're trying to do with it.

You said
But, it doesn't take me to Cart.jsp page.


Where is it taking you?
Is it blowing up?
If so what errors are you seeing?
Where are you seeing them, on the screen, in the log files?

Rather than post the JSP code (which is very hard to read with all those out.print statements), maybe you could post the generated HTML source.
If the forms are not posting to the correct page, it could be that the HTML is not well formed.
 
Ram Kas
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I removed the out.println s and now my code looks as follows:

Now, I get the following error:

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 11 in the jsp file: /show.jsp

Generated servlet error:
[javac] Compiling 1 source file

C:\Program Files\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhost\jsp-examples\org\apache\jsp\show_jsp.java:102: illegal start of expression
<form method = "POST" action ="Cart.jsp">
^



An error occurred at line: 11 in the jsp file: /show.jsp

Generated servlet error:
C:\Program Files\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhost\jsp-examples\org\apache\jsp\show_jsp.java:119: ';' expected
}
^
2 errors



Thanks for your response.
Dinakar Kasturi
 
Ram Kas
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
code is as follows:

 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you've missed my point.

Everything outside of your scriptlet <% ... %> is written to the webpage (no need for out.println statements).
You can't just throw HTML tags inside a scriptlet.


My earlier point was that your resulting HTML may have been malfomrmed.
This could have been causing your forms to fail.
What errors were you seeing earlier.

If you have a way to put the page back together easily, please do so, hit the page, right click on the page, choose View -> HTML Source, and post that.

Otherwise, you may want to take a look at some JSP tutorials to see how to mix scriptlet code with your HTML.



Another 'by the way': Scriptlets have been replaced altogether in JSP 2.0.
These days it is considered a best practice to move all of your business logic (database access, etc) to servlets and beans, and then use JSTL and EL for iterating through the resulting data for formatting.
 
Ram Kas
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

I replaced the code.

The HTML Code is as follows:



Now, when I submit the "Add to Cart" button, it should take me to Cart.jsp where I am just printing the value of the textbox(for now).

I will be using Beans and JSTL as upgradation of this project. As of now, I am OK with scriptlets.

Thanks a lot for your advice.
Dinakar Kasturi.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You're not closing the input field tag.
 
Ram Kas
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added the ending tag for <input>. Even Now, it doesn't display anything.



Any more inputs?

Thanks,
Dinakar Kasturi.
 
Ram Kas
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Ben

I could resolve the issue.

Thanks a lot.
Dinakar Kasturi.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad you've got it.
There are several issues with your HTML.

1.) Make sure you close one form before starting another one.

2.) Either put a table in a form or a form in a table.
Avoid
 
Ram Kas
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, I will follow your guide lines. Also, an you please elaborate on how to use servlets,beans and JSTL together in the project I am doing?

Any links or tips will be great.

Dinakar Kasturi.
 
Ram Kas
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iam trying to create a Shopping Cart. I am stuck with as how to maintain session.

This is the code I have written.




And the error I get is
ype Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 11 in the jsp file: /show.jsp

Generated servlet error:
[javac] Compiling 1 source file

C:\Program Files\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhost\jsp-examples\org\apache\jsp\show_jsp.java:99: cannot resolve symbol
symbol : class Cart
location: class org.apache.jsp.show_jsp
Cart c = (Cart)session.getAttribute("cart");
^



An error occurred at line: 11 in the jsp file: /show.jsp

Generated servlet error:
C:\Program Files\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhost\jsp-examples\org\apache\jsp\show_jsp.java:99: cannot resolve symbol
symbol : class Cart
location: class org.apache.jsp.show_jsp
Cart c = (Cart)session.getAttribute("cart");
^



An error occurred at line: 11 in the jsp file: /show.jsp

Generated servlet error:
C:\Program Files\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhost\jsp-examples\org\apache\jsp\show_jsp.java:102: cannot resolve symbol
symbol : class Cart
location: class org.apache.jsp.show_jsp
c = new Cart();
^



An error occurred at line: 11 in the jsp file: /show.jsp

Generated servlet error:
C:\Program Files\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhost\jsp-examples\org\apache\jsp\show_jsp.java:104: cannot resolve symbol
symbol : variable cart
location: class org.apache.jsp.show_jsp
cart.setCategoryId(categoryId);
^



An error occurred at line: 11 in the jsp file: /show.jsp

Generated servlet error:
C:\Program Files\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhost\jsp-examples\org\apache\jsp\show_jsp.java:105: cannot resolve symbol
symbol : variable cart
location: class org.apache.jsp.show_jsp
cart.setProductId(productId);
^



An error occurred at line: 11 in the jsp file: /show.jsp

Generated servlet error:
C:\Program Files\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhost\jsp-examples\org\apache\jsp\show_jsp.java:106: cannot resolve symbol
symbol : variable cart
location: class org.apache.jsp.show_jsp
session.setAttibute("cart",cart);
^
6 errors


org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:127)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:351)
org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:415)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:458)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:439)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:552)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:291)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)



Please help me to resolve the issue.

Thanks,
Dinakar Kasturi.
 
Ram Kas
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, the following is the error:

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 11 in the jsp file: /show.jsp

Generated servlet error:
[javac] Compiling 1 source file

C:\Program Files\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhost\jsp-examples\org\apache\jsp\show_jsp.java:99: cannot resolve symbol
symbol : class Cart
location: class org.apache.jsp.show_jsp
Cart cart = (Cart)session.getAttribute("cart");
^



An error occurred at line: 11 in the jsp file: /show.jsp

Generated servlet error:
C:\Program Files\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhost\jsp-examples\org\apache\jsp\show_jsp.java:99: cannot resolve symbol
symbol : class Cart
location: class org.apache.jsp.show_jsp
Cart cart = (Cart)session.getAttribute("cart");
^



An error occurred at line: 11 in the jsp file: /show.jsp

Generated servlet error:
C:\Program Files\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhost\jsp-examples\org\apache\jsp\show_jsp.java:102: cannot resolve symbol
symbol : class Cart
location: class org.apache.jsp.show_jsp
cart = new Cart();
^
3 errors


org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:127)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:351)
org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:415)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:458)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:439)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:552)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:291)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)

note The full stack trace of the root cause is available in the Tomcat logs.
Apache Tomcat/5.0.18
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
 
Ram Kas
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I created Cart class.

Its code is as follows:



Code for other files are

index.jsp

show.jsp



cart.jsp

Erroris
HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 3 in the jsp file: /Cart.jsp

Generated servlet error:
[javac] Compiling 1 source file

C:\Program Files\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhost\jsp-examples\org\apache\jsp\Cart_jsp.java:58: cart is already defined in _jspService(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
Cart cart = (Cart)session.getAttribute("cart");
^
1 error


org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:127)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:351)
org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:415)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:458)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:439)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:552)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:291)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)

note The full stack trace of the root cause is available in the Tomcat logs.
Apache Tomcat/5.0.18 [/QUOTE

It will be really great if you can tell me as how to proceed.

Thanks,
Dinakar Kasturi
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dinakar Kasturi:
It will be really great if you can tell me as how to proceed.


I think the best advice at this point is to pay close attention to the compiler messages.



It shouldn't take a stranger on a forum to point that out. ;)
[ September 23, 2006: Message edited by: Ben Souther ]
 
Ram Kas
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand the error pointed out by compiler. But, I don't understand as how to fix it because if I don't create it in the earlier case, compiler says that cart is unresolved.


Thanks,
Dinakar Kasturi
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You've declared the cart variable twice.
 
Ram Kas
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In my shopping cart application, I am able to add only 1 item. So, can you tell me the logic as how to proceed.

My files are as follows:

show.jsp

Cart1.jsp




Thanks in advance.
Dinakar Kasturi.
 
maya pedersen
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to add more than one item to the Cart, the best way of doing this is to create a specific class for the shopping item and then add this to a collection of sorts in the Cart:

public class ShoppingItem {
String productId;
String categoryId;
String NOI;
.
.
.
}

public class Cart {
private ArrayList itemsList(); // list of ShoppingItem
public addItem(ShoppingItem item) {
itemsList.add(item);
}
}

But this is basic Java stuff; if you're having problems with this, perhaps you should hit the books and tutorials before engaging the more tedious task of reading from/writing to a database?

-Maya
 
Richard Green
Ranch Hand
Posts: 536
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ben Souther:
Glad you've got it.
There are several issues with your HTML.

1.) Make sure you close one form before starting another one.

2.) Either put a table in a form or a form in a table.
Avoid

or simply validate your html using http://validator.w3.org/
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic