1

I am fairly new in JSP and I am trying to figure out how to pass data entered in the form on a JSP page to the java class and send this data back to the JSP page on click.

My index.jsp looks a bit like this:

<%@ page import="mypackage.*" %>
<% myClass c = new myClass(); 
c.setString("String"); %>
<p>This is a test: <%= c.getString(); %></p>

The above code will output "String". I can access my class with no problems if I set the value on page load. I tried using servlets after some research. I modified my form to add the servlet "testServlet" on form action:

<form method="POST" action="testServlet">

Then on the doPost() method in testServlet I added in this:

String myString = request.getParameter("myString"); //myString is also the name of my textbox
System.out.println("Entered string: " + myString);

However, I am clearly missing a crucial part of the flow of how this should work and I am most probably wrong with this one as well as all the form does after I press submit is redirect to testServlet so I get the error that the resource is not available since it isn't a JSP/html page.

So my questions are, how exactly can I pass data from JSP to java and vice versa? Also, is there a possible way to do this without servlets? And what are good tutorials/examples for studying JSP and its behavior such as passing data? Please help.

After some more research, I am now able to transfer data set on index to detail using jsp:useBean however, as I've read in all the forums I've visited, servlets should be used to handle this but this really confused me more as a beginner so I really want to figure out what I'm missing here.

I've made sure that the servlet is registered on web.xml and I didn't change anything in there.

Update: I've tried re-creating the project from scratch and somehow managed to make the servlet work. As it turns out, I was missing something on the doGet() method. But now my question stands, is there a way to process the form without using servlets or page import in the JSP file? I was shown a sample code that didn't use servlets or page imports. I did take notice at tag.

user1597438
  • 2,171
  • 5
  • 35
  • 78
  • have you registered `/testServlet` in `web.xml` url pattern OR using Annotaion `@WebServlet("/testServlet")`? and also take a look on [this](http://stackoverflow.com/questions/5374981/how-to-pass-a-value-of-a-variable-from-a-java-class-to-the-jsp-page?rq=1) and [this](http://stackoverflow.com/questions/5414600/pass-data-from-java-servlet-to-jsp?rq=1) – Abhishek Nayak Mar 21 '14 at 02:40
  • It was automatically registered in the web.xml so I didn't change anything there anymore. Anyway, I have tried using JSP:usebean instead which seems to work fine except, there has to be a JSP page between the index and landing page before this works otherwise, the values will be null. I'll update my post to show the changes I made – user1597438 Mar 21 '14 at 02:42
  • 1
    you should use a Servlet for that. don't use a jsp to process your bussiness logic or don't keep any java code on jsp, jsp is a view, Servlet is the controller mean you should keep all java code in servlet, so in future you can debug the code. But not in jsp. – Abhishek Nayak Mar 21 '14 at 02:48
  • Yes, that is what I've read so far. I am still in the process of figuring out using the servlets. I've read something about using Ajax but that confused me even further. – user1597438 Mar 21 '14 at 02:54
  • you should add that part of code to your question where you got confused, then we can help you to understand. also put your `Servlet` to question and if you have `web.xml` – Abhishek Nayak Mar 21 '14 at 02:59

1 Answers1

1

use the java beans with getters and setters for there class member variables.