0

I am trying to pass a string variable called seq from a JSP to a Java program, and pass it on to the another Java program by passing the string as an argument to its object. I am somehow getting stuck.

Start.jsp:

<%@ page import="org.dypbbi.nirmiti.ProtModMain %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>NIRMITI</title>
    </head>
    <body>
        <h1>Please wait...</h1>
        <%
            String seq=request.getParameter("s");
            ProtModMain.getSequence(seq);
        %>
    </body>
</html>

ProtModMain.java:

package org.dypbbi.nirmiti;
public class ProtModMain {
    String sequence="";

    public static String getSequence(String str)
    {
        return str;
    }


    public static void main(String args[])throws Exception
    {
      ProtModMain MainObj = new ProtModMain();
      sequence = MainObj.getSequence();
      new ObjectFactory(sequence);
    }
}

Start.jsp will retrieve the string value from the HTML. It passes the string to ProtModMain class via the method getSequence. I now need to use the string value to pass it to other classes that require it, so I intend to pass it a parameter to the ObjectFactory object. But before that, I need to call the getSequence method in the ProtModMain class so that I can pass the value. I am not understanding how to call the getSequence method in the main method of ProtModMain class.

2
  • 2
    You have a misconception of how jsps work and how they should be used. Take a look at the wiki for both jsp and servlets Commented Apr 15, 2013 at 18:28
  • 1
    How come a main method in a web application! Commented Apr 15, 2013 at 18:31

3 Answers 3

1

You need to set the parameter to the request using request.setAttribute("<name>",<value>). Then you can get it in the Java file using request.getAttribute("<name>").

Reference - Oracle Docs - HttpServletRequest

Sign up to request clarification or add additional context in comments.

2 Comments

request has no "setParameter" method
Referring to the oracle docs link the right way is to use getAttribute and setAttribute methods. request.setAttribute("<name>",<value>) and request.getAttribute("<name>") Correcting Answer.
0

You are not calling the main method. In your JSP you are calling only the static getSequence that, by the way, only returns the value.

I think you have a project concept problem: why your web (JSP) app has a main class?

I think you should adapt:

Start.jsp:

<%@ page import="org.dypbbi.nirmiti.ProtModMain %> 
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>NIRMITI</title>
    </head>
    <body>
        <h1>Please wait...</h1>
        <%
        String seq=request.getParameter("s");
        ProtModMain protModMain = new ProtModMain();
        ObjectFactory myFactory = protModMain.createFactory(seq);
        //do whatever you want with your factory ;)
        %>
    </body>
</html>

ProtModMain.java:

package org.dypbbi.nirmiti;
public class ProtModMain {

    public ObjectFactory createFactory(final String sequence) {
        return new ObjectFactory(sequence);
    }
}

This way you will be calling the methods you intended to.

2 Comments

Thanks. I was thinking, what if I avoid the ProtModMain.java class all together. I can pass the value directly to the ObjectFactory class and avoid the middle man, right? String seq=request.getParameter("s"); new ObjectFactory(seq); the ObjectFactory class will directly receive the string value. This will work right?
@kuks Yes, will work and, if you are only instantiating ProdModMain to get a ObjectFactory instance, I think that it is much more clean to get to the ObjectFactory directly. You could avoid even the variable: new ObjectFactory(request.getParameters("s"));
0

You can use <form> tags and <input type='hidden'> with an <input type='submit'> button, in the form you will specify the method to send and to where send the data.

Or you can store in POJOs and store in session and recover it with a servlet.

Or use Ajax with an XmlHttpRequest.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.