Send parameters to java class from servlet











up vote
1
down vote

favorite












I have a Jsp page and servlet which basically accepts the value for number of cities and next for matrix. My servlet takes the input value as string convert it into integer and store it in a two dimensional array




Index.jsp




<body>
<form action="sample" method="post">
<h1>Travelling Salesman Problem</h1>
<input placeholder="Number of Cities" type="text" name="cities" required="">
<input placeholder="Matrix" type="text" name="matrix" required="">
<button>Submit</button>
</form>
</body>



Servlet




public class sample extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

int city = Integer.parseInt(request.getParameter("cities"));
String numbers = request.getParameter("matrix");
String splitText = numbers.split(" ");
int mat = new int[splitText.length];
for (int i = 0; i < splitText.length; i++) {
mat[i] = Integer.parseInt(splitText[i]);

}

int array2d = new int[city][city];
int count = 0;
for (int i = 0; i < city; i++) {
for (int j = 0; j < city; j++) {

if (count == mat.length)
break;
array2d[i][j] = mat[i * city + j];
count++;


}

}

}
}


Now i want to pass these parameters city and array2d to the following java class



public class TSPNearestNeighbour
{
private int numberOfNodes;
private Stack<Integer> stack;

public TSPNearestNeighbour()
{
stack = new Stack<Integer>();
}
public void tsp(int adjacencyMatrix)
{
numberOfNodes = adjacencyMatrix[1].length - 1;
int visited = new
int[numberOfNodes + 1];
visited[1] = 1;
stack.push(1);
int element, dst = 0, i;
int min = Integer.MAX_VALUE;
boolean minFlag = false;
System.out.print(1 + "t");

while (!stack.isEmpty())
{
element = stack.peek();
i = 1;
min = Integer.MAX_VALUE;
while (i <= numberOfNodes)
{
if
(adjacencyMatrix[element][i] > 1 && visited[i] == 0)
{



if (min > adjacencyMatrix[element][i])
{
min = adjacencyMatrix[element][i];
dst = i;
minFlag = true;
}
}
i++;
}
if (minFlag)
{
visited[dst] = 1;
stack.push(dst);
System.out.print(dst +"t");
minFlag = false;
continue;
}


stack.pop();
}
}

public static void main (String... arg)
{
int number_of_nodes;
Scanner scanner = null;
try
{



number_of_nodes = city //here i want to pass the city parameter




int adjacency_matrix = new int[number_of_nodes + 1][number_of_nodes +1];



for (int i = 1; i <=number_of_nodes; i++)
{
for (int j = 1; j <=number_of_nodes; j++)
{



adjacency_matrix[i][j]= array2d[i][j] // and here the array 2d parameter




}
}
for (int i = 1; i <=number_of_nodes; i++)
{
for (int j = 1; j <=number_of_nodes; j++)
{
if
(adjacency_matrix[i][j] == 1 && adjacency_matrix[j][i] == 0)
{


adjacency_matrix[j][i] = 1;
}
}
}
System.out.println("the citys are visited as follows");
TSPNearestNeighbour tspNearestNeighbour = new TSPNearestNeighbour();
tspNearestNeighbour.tsp(adjacency_matrix);
} catch (InputMismatchException inputMismatch)
{
System.out.println("Wrong Input format");
}
scanner.close();
}
}


So how can we pass on these parameters from a servlet?
And where to include this class? In the servlet itself or create a separate class, as I'm using intellij. So please guide me through this.










share|improve this question
























  • Where is that "program" running?
    – JGlass
    Nov 21 at 15:47










  • Where do you want to pass them?
    – Jonathan Laliberte
    Nov 21 at 15:52










  • Actually this is where even I'm confused.. If I separately run the program by creating separate .java file in intellij and run. It's working fine by accepting command line inputs. So I wanted a web interface for that and my progress is this. So how to pass these inputted parameters exactly to program? So, please guide me on this.
    – Abhishek
    Nov 21 at 16:04










  • @Jonathan I have highlighted the place where I want to pass the argument,please check
    – Abhishek
    Nov 21 at 16:06















up vote
1
down vote

favorite












I have a Jsp page and servlet which basically accepts the value for number of cities and next for matrix. My servlet takes the input value as string convert it into integer and store it in a two dimensional array




Index.jsp




<body>
<form action="sample" method="post">
<h1>Travelling Salesman Problem</h1>
<input placeholder="Number of Cities" type="text" name="cities" required="">
<input placeholder="Matrix" type="text" name="matrix" required="">
<button>Submit</button>
</form>
</body>



Servlet




public class sample extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

int city = Integer.parseInt(request.getParameter("cities"));
String numbers = request.getParameter("matrix");
String splitText = numbers.split(" ");
int mat = new int[splitText.length];
for (int i = 0; i < splitText.length; i++) {
mat[i] = Integer.parseInt(splitText[i]);

}

int array2d = new int[city][city];
int count = 0;
for (int i = 0; i < city; i++) {
for (int j = 0; j < city; j++) {

if (count == mat.length)
break;
array2d[i][j] = mat[i * city + j];
count++;


}

}

}
}


Now i want to pass these parameters city and array2d to the following java class



public class TSPNearestNeighbour
{
private int numberOfNodes;
private Stack<Integer> stack;

public TSPNearestNeighbour()
{
stack = new Stack<Integer>();
}
public void tsp(int adjacencyMatrix)
{
numberOfNodes = adjacencyMatrix[1].length - 1;
int visited = new
int[numberOfNodes + 1];
visited[1] = 1;
stack.push(1);
int element, dst = 0, i;
int min = Integer.MAX_VALUE;
boolean minFlag = false;
System.out.print(1 + "t");

while (!stack.isEmpty())
{
element = stack.peek();
i = 1;
min = Integer.MAX_VALUE;
while (i <= numberOfNodes)
{
if
(adjacencyMatrix[element][i] > 1 && visited[i] == 0)
{



if (min > adjacencyMatrix[element][i])
{
min = adjacencyMatrix[element][i];
dst = i;
minFlag = true;
}
}
i++;
}
if (minFlag)
{
visited[dst] = 1;
stack.push(dst);
System.out.print(dst +"t");
minFlag = false;
continue;
}


stack.pop();
}
}

public static void main (String... arg)
{
int number_of_nodes;
Scanner scanner = null;
try
{



number_of_nodes = city //here i want to pass the city parameter




int adjacency_matrix = new int[number_of_nodes + 1][number_of_nodes +1];



for (int i = 1; i <=number_of_nodes; i++)
{
for (int j = 1; j <=number_of_nodes; j++)
{



adjacency_matrix[i][j]= array2d[i][j] // and here the array 2d parameter




}
}
for (int i = 1; i <=number_of_nodes; i++)
{
for (int j = 1; j <=number_of_nodes; j++)
{
if
(adjacency_matrix[i][j] == 1 && adjacency_matrix[j][i] == 0)
{


adjacency_matrix[j][i] = 1;
}
}
}
System.out.println("the citys are visited as follows");
TSPNearestNeighbour tspNearestNeighbour = new TSPNearestNeighbour();
tspNearestNeighbour.tsp(adjacency_matrix);
} catch (InputMismatchException inputMismatch)
{
System.out.println("Wrong Input format");
}
scanner.close();
}
}


So how can we pass on these parameters from a servlet?
And where to include this class? In the servlet itself or create a separate class, as I'm using intellij. So please guide me through this.










share|improve this question
























  • Where is that "program" running?
    – JGlass
    Nov 21 at 15:47










  • Where do you want to pass them?
    – Jonathan Laliberte
    Nov 21 at 15:52










  • Actually this is where even I'm confused.. If I separately run the program by creating separate .java file in intellij and run. It's working fine by accepting command line inputs. So I wanted a web interface for that and my progress is this. So how to pass these inputted parameters exactly to program? So, please guide me on this.
    – Abhishek
    Nov 21 at 16:04










  • @Jonathan I have highlighted the place where I want to pass the argument,please check
    – Abhishek
    Nov 21 at 16:06













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have a Jsp page and servlet which basically accepts the value for number of cities and next for matrix. My servlet takes the input value as string convert it into integer and store it in a two dimensional array




Index.jsp




<body>
<form action="sample" method="post">
<h1>Travelling Salesman Problem</h1>
<input placeholder="Number of Cities" type="text" name="cities" required="">
<input placeholder="Matrix" type="text" name="matrix" required="">
<button>Submit</button>
</form>
</body>



Servlet




public class sample extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

int city = Integer.parseInt(request.getParameter("cities"));
String numbers = request.getParameter("matrix");
String splitText = numbers.split(" ");
int mat = new int[splitText.length];
for (int i = 0; i < splitText.length; i++) {
mat[i] = Integer.parseInt(splitText[i]);

}

int array2d = new int[city][city];
int count = 0;
for (int i = 0; i < city; i++) {
for (int j = 0; j < city; j++) {

if (count == mat.length)
break;
array2d[i][j] = mat[i * city + j];
count++;


}

}

}
}


Now i want to pass these parameters city and array2d to the following java class



public class TSPNearestNeighbour
{
private int numberOfNodes;
private Stack<Integer> stack;

public TSPNearestNeighbour()
{
stack = new Stack<Integer>();
}
public void tsp(int adjacencyMatrix)
{
numberOfNodes = adjacencyMatrix[1].length - 1;
int visited = new
int[numberOfNodes + 1];
visited[1] = 1;
stack.push(1);
int element, dst = 0, i;
int min = Integer.MAX_VALUE;
boolean minFlag = false;
System.out.print(1 + "t");

while (!stack.isEmpty())
{
element = stack.peek();
i = 1;
min = Integer.MAX_VALUE;
while (i <= numberOfNodes)
{
if
(adjacencyMatrix[element][i] > 1 && visited[i] == 0)
{



if (min > adjacencyMatrix[element][i])
{
min = adjacencyMatrix[element][i];
dst = i;
minFlag = true;
}
}
i++;
}
if (minFlag)
{
visited[dst] = 1;
stack.push(dst);
System.out.print(dst +"t");
minFlag = false;
continue;
}


stack.pop();
}
}

public static void main (String... arg)
{
int number_of_nodes;
Scanner scanner = null;
try
{



number_of_nodes = city //here i want to pass the city parameter




int adjacency_matrix = new int[number_of_nodes + 1][number_of_nodes +1];



for (int i = 1; i <=number_of_nodes; i++)
{
for (int j = 1; j <=number_of_nodes; j++)
{



adjacency_matrix[i][j]= array2d[i][j] // and here the array 2d parameter




}
}
for (int i = 1; i <=number_of_nodes; i++)
{
for (int j = 1; j <=number_of_nodes; j++)
{
if
(adjacency_matrix[i][j] == 1 && adjacency_matrix[j][i] == 0)
{


adjacency_matrix[j][i] = 1;
}
}
}
System.out.println("the citys are visited as follows");
TSPNearestNeighbour tspNearestNeighbour = new TSPNearestNeighbour();
tspNearestNeighbour.tsp(adjacency_matrix);
} catch (InputMismatchException inputMismatch)
{
System.out.println("Wrong Input format");
}
scanner.close();
}
}


So how can we pass on these parameters from a servlet?
And where to include this class? In the servlet itself or create a separate class, as I'm using intellij. So please guide me through this.










share|improve this question















I have a Jsp page and servlet which basically accepts the value for number of cities and next for matrix. My servlet takes the input value as string convert it into integer and store it in a two dimensional array




Index.jsp




<body>
<form action="sample" method="post">
<h1>Travelling Salesman Problem</h1>
<input placeholder="Number of Cities" type="text" name="cities" required="">
<input placeholder="Matrix" type="text" name="matrix" required="">
<button>Submit</button>
</form>
</body>



Servlet




public class sample extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

int city = Integer.parseInt(request.getParameter("cities"));
String numbers = request.getParameter("matrix");
String splitText = numbers.split(" ");
int mat = new int[splitText.length];
for (int i = 0; i < splitText.length; i++) {
mat[i] = Integer.parseInt(splitText[i]);

}

int array2d = new int[city][city];
int count = 0;
for (int i = 0; i < city; i++) {
for (int j = 0; j < city; j++) {

if (count == mat.length)
break;
array2d[i][j] = mat[i * city + j];
count++;


}

}

}
}


Now i want to pass these parameters city and array2d to the following java class



public class TSPNearestNeighbour
{
private int numberOfNodes;
private Stack<Integer> stack;

public TSPNearestNeighbour()
{
stack = new Stack<Integer>();
}
public void tsp(int adjacencyMatrix)
{
numberOfNodes = adjacencyMatrix[1].length - 1;
int visited = new
int[numberOfNodes + 1];
visited[1] = 1;
stack.push(1);
int element, dst = 0, i;
int min = Integer.MAX_VALUE;
boolean minFlag = false;
System.out.print(1 + "t");

while (!stack.isEmpty())
{
element = stack.peek();
i = 1;
min = Integer.MAX_VALUE;
while (i <= numberOfNodes)
{
if
(adjacencyMatrix[element][i] > 1 && visited[i] == 0)
{



if (min > adjacencyMatrix[element][i])
{
min = adjacencyMatrix[element][i];
dst = i;
minFlag = true;
}
}
i++;
}
if (minFlag)
{
visited[dst] = 1;
stack.push(dst);
System.out.print(dst +"t");
minFlag = false;
continue;
}


stack.pop();
}
}

public static void main (String... arg)
{
int number_of_nodes;
Scanner scanner = null;
try
{



number_of_nodes = city //here i want to pass the city parameter




int adjacency_matrix = new int[number_of_nodes + 1][number_of_nodes +1];



for (int i = 1; i <=number_of_nodes; i++)
{
for (int j = 1; j <=number_of_nodes; j++)
{



adjacency_matrix[i][j]= array2d[i][j] // and here the array 2d parameter




}
}
for (int i = 1; i <=number_of_nodes; i++)
{
for (int j = 1; j <=number_of_nodes; j++)
{
if
(adjacency_matrix[i][j] == 1 && adjacency_matrix[j][i] == 0)
{


adjacency_matrix[j][i] = 1;
}
}
}
System.out.println("the citys are visited as follows");
TSPNearestNeighbour tspNearestNeighbour = new TSPNearestNeighbour();
tspNearestNeighbour.tsp(adjacency_matrix);
} catch (InputMismatchException inputMismatch)
{
System.out.println("Wrong Input format");
}
scanner.close();
}
}


So how can we pass on these parameters from a servlet?
And where to include this class? In the servlet itself or create a separate class, as I'm using intellij. So please guide me through this.







java jsp web servlets java-ee






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 16:47

























asked Nov 21 at 15:26









Abhishek

198




198












  • Where is that "program" running?
    – JGlass
    Nov 21 at 15:47










  • Where do you want to pass them?
    – Jonathan Laliberte
    Nov 21 at 15:52










  • Actually this is where even I'm confused.. If I separately run the program by creating separate .java file in intellij and run. It's working fine by accepting command line inputs. So I wanted a web interface for that and my progress is this. So how to pass these inputted parameters exactly to program? So, please guide me on this.
    – Abhishek
    Nov 21 at 16:04










  • @Jonathan I have highlighted the place where I want to pass the argument,please check
    – Abhishek
    Nov 21 at 16:06


















  • Where is that "program" running?
    – JGlass
    Nov 21 at 15:47










  • Where do you want to pass them?
    – Jonathan Laliberte
    Nov 21 at 15:52










  • Actually this is where even I'm confused.. If I separately run the program by creating separate .java file in intellij and run. It's working fine by accepting command line inputs. So I wanted a web interface for that and my progress is this. So how to pass these inputted parameters exactly to program? So, please guide me on this.
    – Abhishek
    Nov 21 at 16:04










  • @Jonathan I have highlighted the place where I want to pass the argument,please check
    – Abhishek
    Nov 21 at 16:06
















Where is that "program" running?
– JGlass
Nov 21 at 15:47




Where is that "program" running?
– JGlass
Nov 21 at 15:47












Where do you want to pass them?
– Jonathan Laliberte
Nov 21 at 15:52




Where do you want to pass them?
– Jonathan Laliberte
Nov 21 at 15:52












Actually this is where even I'm confused.. If I separately run the program by creating separate .java file in intellij and run. It's working fine by accepting command line inputs. So I wanted a web interface for that and my progress is this. So how to pass these inputted parameters exactly to program? So, please guide me on this.
– Abhishek
Nov 21 at 16:04




Actually this is where even I'm confused.. If I separately run the program by creating separate .java file in intellij and run. It's working fine by accepting command line inputs. So I wanted a web interface for that and my progress is this. So how to pass these inputted parameters exactly to program? So, please guide me on this.
– Abhishek
Nov 21 at 16:04












@Jonathan I have highlighted the place where I want to pass the argument,please check
– Abhishek
Nov 21 at 16:06




@Jonathan I have highlighted the place where I want to pass the argument,please check
– Abhishek
Nov 21 at 16:06












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










I'm seeing some logical errors in your code, but I'm not going into that context and I don't know what you are trying to do(On the processing side). But I'll try to cover the actual question you are asking, like "how to pass the jsp parameters to your another class".



So you can directly pass the strings as an argument to the another class like I've shown in the below code. I think I don't need to explain further you can see the code you will get it.



First your Servlet code: Sample.java



package com.server.testing;

import java.io.IOException;

public class Sample extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String city = request.getParameter("cities");
String numbers = request.getParameter("matrix");

String args = new String[2];
args[0] = city;
args[1] = numbers;
TSPNearestNeighbour.main(args);

}
}


Now your another java class: TSPNearestNeighbour.java



package com.server.testing;

import java.util.InputMismatchException;
import java.util.Stack;

public class TSPNearestNeighbour {
private int numberOfNodes;
private Stack<Integer> stack;

public TSPNearestNeighbour() {
stack = new Stack<Integer>();
}

public void tsp(int adjacencyMatrix) {
numberOfNodes = adjacencyMatrix[1].length - 1;
int visited = new int[numberOfNodes + 1];
visited[1] = 1;
stack.push(1);
int element, dst = 0, i;
int min = Integer.MAX_VALUE;
boolean minFlag = false;
System.out.print(1 + "t");

while (!stack.isEmpty()) {
element = stack.peek();
i = 1;
min = Integer.MAX_VALUE;
while (i <= numberOfNodes) {
if (adjacencyMatrix[element][i] > 1 && visited[i] == 0) {

if (min > adjacencyMatrix[element][i]) {
min = adjacencyMatrix[element][i];
dst = i;
minFlag = true;
}
}
i++;
}
if (minFlag) {
visited[dst] = 1;
stack.push(dst);
System.out.print(dst + "t");
minFlag = false;
continue;
}

stack.pop();
}
}

public static void main(String args) {
if(args.length<2) {
System.out.println("Two arguments required <city> <numbers>");
System.exit(-1);
}

int number_of_nodes=Integer.parseInt(args[0]);
String splitText = args[1].split(" +");
int mat = new int[splitText.length];
for (int i = 0; i < splitText.length; i++) {
mat[i] = Integer.parseInt(splitText[i]);

}



try {
int adjacency_matrix = new int[number_of_nodes + 1][number_of_nodes + 1];
int count = 0;
for (int i = 1; i <= number_of_nodes; i++) {
for (int j = 1; j <= number_of_nodes; j++) {
if (count == mat.length)
break;
adjacency_matrix[i][j]= mat[(i-1) * number_of_nodes + (j-1)];
count++;
}
}
for (int i = 1; i <= number_of_nodes; i++) {
for (int j = 1; j <= number_of_nodes; j++) {
if (adjacency_matrix[i][j] == 1 && adjacency_matrix[j][i] == 0) {

adjacency_matrix[j][i] = 1;
}
}
}
System.out.println("the citys are visited as follows");
TSPNearestNeighbour tspNearestNeighbour = new TSPNearestNeighbour();
tspNearestNeighbour.tsp(adjacency_matrix);
} catch (InputMismatchException inputMismatch) {
System.out.println("Wrong Input format");
}

}
}





share|improve this answer





















  • According to my logic the java class is working fine. I just wanted to know regarding passing values. So, i will try what you have explained and let me see.
    – Abhishek
    Nov 22 at 16:02










  • Okay let me know if it worked (and you can accept the answer if it worked)
    – abhishek chaurasiya
    Nov 23 at 5:07










  • I used your concept and it's working.
    – Abhishek
    2 days ago











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415305%2fsend-parameters-to-java-class-from-servlet%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










I'm seeing some logical errors in your code, but I'm not going into that context and I don't know what you are trying to do(On the processing side). But I'll try to cover the actual question you are asking, like "how to pass the jsp parameters to your another class".



So you can directly pass the strings as an argument to the another class like I've shown in the below code. I think I don't need to explain further you can see the code you will get it.



First your Servlet code: Sample.java



package com.server.testing;

import java.io.IOException;

public class Sample extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String city = request.getParameter("cities");
String numbers = request.getParameter("matrix");

String args = new String[2];
args[0] = city;
args[1] = numbers;
TSPNearestNeighbour.main(args);

}
}


Now your another java class: TSPNearestNeighbour.java



package com.server.testing;

import java.util.InputMismatchException;
import java.util.Stack;

public class TSPNearestNeighbour {
private int numberOfNodes;
private Stack<Integer> stack;

public TSPNearestNeighbour() {
stack = new Stack<Integer>();
}

public void tsp(int adjacencyMatrix) {
numberOfNodes = adjacencyMatrix[1].length - 1;
int visited = new int[numberOfNodes + 1];
visited[1] = 1;
stack.push(1);
int element, dst = 0, i;
int min = Integer.MAX_VALUE;
boolean minFlag = false;
System.out.print(1 + "t");

while (!stack.isEmpty()) {
element = stack.peek();
i = 1;
min = Integer.MAX_VALUE;
while (i <= numberOfNodes) {
if (adjacencyMatrix[element][i] > 1 && visited[i] == 0) {

if (min > adjacencyMatrix[element][i]) {
min = adjacencyMatrix[element][i];
dst = i;
minFlag = true;
}
}
i++;
}
if (minFlag) {
visited[dst] = 1;
stack.push(dst);
System.out.print(dst + "t");
minFlag = false;
continue;
}

stack.pop();
}
}

public static void main(String args) {
if(args.length<2) {
System.out.println("Two arguments required <city> <numbers>");
System.exit(-1);
}

int number_of_nodes=Integer.parseInt(args[0]);
String splitText = args[1].split(" +");
int mat = new int[splitText.length];
for (int i = 0; i < splitText.length; i++) {
mat[i] = Integer.parseInt(splitText[i]);

}



try {
int adjacency_matrix = new int[number_of_nodes + 1][number_of_nodes + 1];
int count = 0;
for (int i = 1; i <= number_of_nodes; i++) {
for (int j = 1; j <= number_of_nodes; j++) {
if (count == mat.length)
break;
adjacency_matrix[i][j]= mat[(i-1) * number_of_nodes + (j-1)];
count++;
}
}
for (int i = 1; i <= number_of_nodes; i++) {
for (int j = 1; j <= number_of_nodes; j++) {
if (adjacency_matrix[i][j] == 1 && adjacency_matrix[j][i] == 0) {

adjacency_matrix[j][i] = 1;
}
}
}
System.out.println("the citys are visited as follows");
TSPNearestNeighbour tspNearestNeighbour = new TSPNearestNeighbour();
tspNearestNeighbour.tsp(adjacency_matrix);
} catch (InputMismatchException inputMismatch) {
System.out.println("Wrong Input format");
}

}
}





share|improve this answer





















  • According to my logic the java class is working fine. I just wanted to know regarding passing values. So, i will try what you have explained and let me see.
    – Abhishek
    Nov 22 at 16:02










  • Okay let me know if it worked (and you can accept the answer if it worked)
    – abhishek chaurasiya
    Nov 23 at 5:07










  • I used your concept and it's working.
    – Abhishek
    2 days ago















up vote
1
down vote



accepted










I'm seeing some logical errors in your code, but I'm not going into that context and I don't know what you are trying to do(On the processing side). But I'll try to cover the actual question you are asking, like "how to pass the jsp parameters to your another class".



So you can directly pass the strings as an argument to the another class like I've shown in the below code. I think I don't need to explain further you can see the code you will get it.



First your Servlet code: Sample.java



package com.server.testing;

import java.io.IOException;

public class Sample extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String city = request.getParameter("cities");
String numbers = request.getParameter("matrix");

String args = new String[2];
args[0] = city;
args[1] = numbers;
TSPNearestNeighbour.main(args);

}
}


Now your another java class: TSPNearestNeighbour.java



package com.server.testing;

import java.util.InputMismatchException;
import java.util.Stack;

public class TSPNearestNeighbour {
private int numberOfNodes;
private Stack<Integer> stack;

public TSPNearestNeighbour() {
stack = new Stack<Integer>();
}

public void tsp(int adjacencyMatrix) {
numberOfNodes = adjacencyMatrix[1].length - 1;
int visited = new int[numberOfNodes + 1];
visited[1] = 1;
stack.push(1);
int element, dst = 0, i;
int min = Integer.MAX_VALUE;
boolean minFlag = false;
System.out.print(1 + "t");

while (!stack.isEmpty()) {
element = stack.peek();
i = 1;
min = Integer.MAX_VALUE;
while (i <= numberOfNodes) {
if (adjacencyMatrix[element][i] > 1 && visited[i] == 0) {

if (min > adjacencyMatrix[element][i]) {
min = adjacencyMatrix[element][i];
dst = i;
minFlag = true;
}
}
i++;
}
if (minFlag) {
visited[dst] = 1;
stack.push(dst);
System.out.print(dst + "t");
minFlag = false;
continue;
}

stack.pop();
}
}

public static void main(String args) {
if(args.length<2) {
System.out.println("Two arguments required <city> <numbers>");
System.exit(-1);
}

int number_of_nodes=Integer.parseInt(args[0]);
String splitText = args[1].split(" +");
int mat = new int[splitText.length];
for (int i = 0; i < splitText.length; i++) {
mat[i] = Integer.parseInt(splitText[i]);

}



try {
int adjacency_matrix = new int[number_of_nodes + 1][number_of_nodes + 1];
int count = 0;
for (int i = 1; i <= number_of_nodes; i++) {
for (int j = 1; j <= number_of_nodes; j++) {
if (count == mat.length)
break;
adjacency_matrix[i][j]= mat[(i-1) * number_of_nodes + (j-1)];
count++;
}
}
for (int i = 1; i <= number_of_nodes; i++) {
for (int j = 1; j <= number_of_nodes; j++) {
if (adjacency_matrix[i][j] == 1 && adjacency_matrix[j][i] == 0) {

adjacency_matrix[j][i] = 1;
}
}
}
System.out.println("the citys are visited as follows");
TSPNearestNeighbour tspNearestNeighbour = new TSPNearestNeighbour();
tspNearestNeighbour.tsp(adjacency_matrix);
} catch (InputMismatchException inputMismatch) {
System.out.println("Wrong Input format");
}

}
}





share|improve this answer





















  • According to my logic the java class is working fine. I just wanted to know regarding passing values. So, i will try what you have explained and let me see.
    – Abhishek
    Nov 22 at 16:02










  • Okay let me know if it worked (and you can accept the answer if it worked)
    – abhishek chaurasiya
    Nov 23 at 5:07










  • I used your concept and it's working.
    – Abhishek
    2 days ago













up vote
1
down vote



accepted







up vote
1
down vote



accepted






I'm seeing some logical errors in your code, but I'm not going into that context and I don't know what you are trying to do(On the processing side). But I'll try to cover the actual question you are asking, like "how to pass the jsp parameters to your another class".



So you can directly pass the strings as an argument to the another class like I've shown in the below code. I think I don't need to explain further you can see the code you will get it.



First your Servlet code: Sample.java



package com.server.testing;

import java.io.IOException;

public class Sample extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String city = request.getParameter("cities");
String numbers = request.getParameter("matrix");

String args = new String[2];
args[0] = city;
args[1] = numbers;
TSPNearestNeighbour.main(args);

}
}


Now your another java class: TSPNearestNeighbour.java



package com.server.testing;

import java.util.InputMismatchException;
import java.util.Stack;

public class TSPNearestNeighbour {
private int numberOfNodes;
private Stack<Integer> stack;

public TSPNearestNeighbour() {
stack = new Stack<Integer>();
}

public void tsp(int adjacencyMatrix) {
numberOfNodes = adjacencyMatrix[1].length - 1;
int visited = new int[numberOfNodes + 1];
visited[1] = 1;
stack.push(1);
int element, dst = 0, i;
int min = Integer.MAX_VALUE;
boolean minFlag = false;
System.out.print(1 + "t");

while (!stack.isEmpty()) {
element = stack.peek();
i = 1;
min = Integer.MAX_VALUE;
while (i <= numberOfNodes) {
if (adjacencyMatrix[element][i] > 1 && visited[i] == 0) {

if (min > adjacencyMatrix[element][i]) {
min = adjacencyMatrix[element][i];
dst = i;
minFlag = true;
}
}
i++;
}
if (minFlag) {
visited[dst] = 1;
stack.push(dst);
System.out.print(dst + "t");
minFlag = false;
continue;
}

stack.pop();
}
}

public static void main(String args) {
if(args.length<2) {
System.out.println("Two arguments required <city> <numbers>");
System.exit(-1);
}

int number_of_nodes=Integer.parseInt(args[0]);
String splitText = args[1].split(" +");
int mat = new int[splitText.length];
for (int i = 0; i < splitText.length; i++) {
mat[i] = Integer.parseInt(splitText[i]);

}



try {
int adjacency_matrix = new int[number_of_nodes + 1][number_of_nodes + 1];
int count = 0;
for (int i = 1; i <= number_of_nodes; i++) {
for (int j = 1; j <= number_of_nodes; j++) {
if (count == mat.length)
break;
adjacency_matrix[i][j]= mat[(i-1) * number_of_nodes + (j-1)];
count++;
}
}
for (int i = 1; i <= number_of_nodes; i++) {
for (int j = 1; j <= number_of_nodes; j++) {
if (adjacency_matrix[i][j] == 1 && adjacency_matrix[j][i] == 0) {

adjacency_matrix[j][i] = 1;
}
}
}
System.out.println("the citys are visited as follows");
TSPNearestNeighbour tspNearestNeighbour = new TSPNearestNeighbour();
tspNearestNeighbour.tsp(adjacency_matrix);
} catch (InputMismatchException inputMismatch) {
System.out.println("Wrong Input format");
}

}
}





share|improve this answer












I'm seeing some logical errors in your code, but I'm not going into that context and I don't know what you are trying to do(On the processing side). But I'll try to cover the actual question you are asking, like "how to pass the jsp parameters to your another class".



So you can directly pass the strings as an argument to the another class like I've shown in the below code. I think I don't need to explain further you can see the code you will get it.



First your Servlet code: Sample.java



package com.server.testing;

import java.io.IOException;

public class Sample extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String city = request.getParameter("cities");
String numbers = request.getParameter("matrix");

String args = new String[2];
args[0] = city;
args[1] = numbers;
TSPNearestNeighbour.main(args);

}
}


Now your another java class: TSPNearestNeighbour.java



package com.server.testing;

import java.util.InputMismatchException;
import java.util.Stack;

public class TSPNearestNeighbour {
private int numberOfNodes;
private Stack<Integer> stack;

public TSPNearestNeighbour() {
stack = new Stack<Integer>();
}

public void tsp(int adjacencyMatrix) {
numberOfNodes = adjacencyMatrix[1].length - 1;
int visited = new int[numberOfNodes + 1];
visited[1] = 1;
stack.push(1);
int element, dst = 0, i;
int min = Integer.MAX_VALUE;
boolean minFlag = false;
System.out.print(1 + "t");

while (!stack.isEmpty()) {
element = stack.peek();
i = 1;
min = Integer.MAX_VALUE;
while (i <= numberOfNodes) {
if (adjacencyMatrix[element][i] > 1 && visited[i] == 0) {

if (min > adjacencyMatrix[element][i]) {
min = adjacencyMatrix[element][i];
dst = i;
minFlag = true;
}
}
i++;
}
if (minFlag) {
visited[dst] = 1;
stack.push(dst);
System.out.print(dst + "t");
minFlag = false;
continue;
}

stack.pop();
}
}

public static void main(String args) {
if(args.length<2) {
System.out.println("Two arguments required <city> <numbers>");
System.exit(-1);
}

int number_of_nodes=Integer.parseInt(args[0]);
String splitText = args[1].split(" +");
int mat = new int[splitText.length];
for (int i = 0; i < splitText.length; i++) {
mat[i] = Integer.parseInt(splitText[i]);

}



try {
int adjacency_matrix = new int[number_of_nodes + 1][number_of_nodes + 1];
int count = 0;
for (int i = 1; i <= number_of_nodes; i++) {
for (int j = 1; j <= number_of_nodes; j++) {
if (count == mat.length)
break;
adjacency_matrix[i][j]= mat[(i-1) * number_of_nodes + (j-1)];
count++;
}
}
for (int i = 1; i <= number_of_nodes; i++) {
for (int j = 1; j <= number_of_nodes; j++) {
if (adjacency_matrix[i][j] == 1 && adjacency_matrix[j][i] == 0) {

adjacency_matrix[j][i] = 1;
}
}
}
System.out.println("the citys are visited as follows");
TSPNearestNeighbour tspNearestNeighbour = new TSPNearestNeighbour();
tspNearestNeighbour.tsp(adjacency_matrix);
} catch (InputMismatchException inputMismatch) {
System.out.println("Wrong Input format");
}

}
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 at 18:23









abhishek chaurasiya

462




462












  • According to my logic the java class is working fine. I just wanted to know regarding passing values. So, i will try what you have explained and let me see.
    – Abhishek
    Nov 22 at 16:02










  • Okay let me know if it worked (and you can accept the answer if it worked)
    – abhishek chaurasiya
    Nov 23 at 5:07










  • I used your concept and it's working.
    – Abhishek
    2 days ago


















  • According to my logic the java class is working fine. I just wanted to know regarding passing values. So, i will try what you have explained and let me see.
    – Abhishek
    Nov 22 at 16:02










  • Okay let me know if it worked (and you can accept the answer if it worked)
    – abhishek chaurasiya
    Nov 23 at 5:07










  • I used your concept and it's working.
    – Abhishek
    2 days ago
















According to my logic the java class is working fine. I just wanted to know regarding passing values. So, i will try what you have explained and let me see.
– Abhishek
Nov 22 at 16:02




According to my logic the java class is working fine. I just wanted to know regarding passing values. So, i will try what you have explained and let me see.
– Abhishek
Nov 22 at 16:02












Okay let me know if it worked (and you can accept the answer if it worked)
– abhishek chaurasiya
Nov 23 at 5:07




Okay let me know if it worked (and you can accept the answer if it worked)
– abhishek chaurasiya
Nov 23 at 5:07












I used your concept and it's working.
– Abhishek
2 days ago




I used your concept and it's working.
– Abhishek
2 days ago


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415305%2fsend-parameters-to-java-class-from-servlet%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks