Issue
I am currently using /Main as the default url-pattern. It is possible to replace this with /Main2 or another word, but I would like to add one directory such as /Project/Main.
/Project path is not really present, but just want /Main and /Project/Main to work the same way.
In other words, it is hoped that the described routes will invoke one servlet. Is there a way?
ex) 172.0.0.1/Project/Main = 172.0.0.1/Main
=> result : Main_servlet
Solution
To my knowledge, you can only pass to the main knowledge in String format. This is because things passed to the main method come from System.in, either through random user input or things like piping, whereby you pass Strings from one Java program to another.
That being said, what you could do is that you create a method in your object class to parse a String form of that object to recreate the original version of that object.
For example:
public class myRectangle
{
private int length;
private int width;
public myRectangle(int inLength, int inWidth)
{
this.length = inLength;
this.width = inWidth;
}
// the rest of your class
public String toString()
{
return "[" + length + ", " + width + "]";
}
public static Rectangle parseString(String input)
{
int firstBracketIndex;
int commaIndex;
int lastBracketIndex;
firstBracketIndex = 0;
commaIndex = input.indexOf(",");
lastBracketIndex = input.length() - 1;
String aWidth = input.substring(firstBracketIndex, (commaIndex - 1));
String aLength = input.substring((commaIndex + 2), lastBracketIndex);
return new Rectangle(Integer.parseInt(aWidth), Integer.parseInt(aLength));
}
}
Something like this could solve your problem. (There may be some off by one errors in my code and I wrote it out really long so it would be clear, but you get the idea!)
The point is, that you create a parsing method that is the inverse of your toString method, so that you can take copies of your class off the command line.
Answered By - alan