kuks
kuks

Reputation: 31

Passing a variable from JSP to Java

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.

Upvotes: 0

Views: 12735

Answers (3)

Sujay
Sujay

Reputation: 94

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

Upvotes: 1

elopez
elopez

Reputation: 109

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.

Upvotes: 0

Fernando M. Pinheiro
Fernando M. Pinheiro

Reputation: 650

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.

Upvotes: 0

Related Questions