为了账号安全,请及时绑定邮箱和手机立即绑定

我不能在 Servlet url-pattern 中添加任何目录路径吗?

我不能在 Servlet url-pattern 中添加任何目录路径吗?

翻翻过去那场雪 2023-03-02 09:58:00
我目前使用 /Main 作为默认的 url-pattern。可以用 /Main2 或其他词替换它,但我想添加一个目录,例如 /Project/Main。/Project 路径实际上并不存在,只是希望 /Main 和 /Project/Main 以相同的方式工作。换句话说,希望所描述的路由将调用一个 servlet。有办法吗?例如)172.0.0.1/项目/主要 = 172.0.0.1/主要=> 结果:Main_servlet
查看完整描述

1 回答

?
慕村225694

TA贡献1880条经验 获得超4个赞

据我所知,您只能以String格式传递给主要知识。这是因为传递给 main 方法的东西来自 System.in,通过随机用户输入或类似管道的东西,您可以通过管道将字符串从一个 Java 程序传递到另一个。


也就是说,您可以做的是在对象类中创建一个方法来解析该对象的 String 形式,从而重新创建该对象的原始版本。


例如:


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));

      }


 }

这样的事情可以解决你的问题。(我的代码中可能会有一些错误,我写得很长所以很清楚,但你明白了!)


关键是,您创建了一个与 toString 方法相反的解析方法,这样您就可以从命令行获取类的副本。


查看完整回答
反对 回复 2023-03-02
  • 1 回答
  • 0 关注
  • 63 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信