您的位置:首页 > Web前端 > HTML

浅谈Servlet读取Html参数

2015-08-31 11:37 369 查看
1首先:webApp名称为cookieAndsession。html文件一般放在WebRoot文件夹下:/cookieAndsession/WebRoot/OrderForm.html,那么外界要访问这个页面时只需直接键入应用名称/*.html2。servelet名称:/cookieAndsession/src/cn/donghua/session/ShowItems.java3. web.xml配置:<servlet>
<servlet-name>showItems</servlet-name>
<servlet-class>cn.donghua.session.ShowItems</servlet-class>
</servlet><servlet-mapping>
<servlet-name>showItems</servlet-name>
<url-pattern>/showItems</url-pattern>
</servlet-mapping>4.html表单名字OrderForm.html.路径:/cookieAndsession/WebRoot/OrderForm.html(放在除了WEB-INF的其他顶层目录)4.1 使用绝对路径:(包含http://)

绝对路径就是带有网址的路径,比如你有一个域名www.dreamdu.com,和一个网站空间,上面的四个文件就可以这么表示。

<
a
href
=
"
http://www.dreamdu.com/exe/1.html
"
>
链接到1.html
</
a
>

<
a
href
=
"
http://www.dreamdu.com/exe/first/2.html
"
>
链接到2.html
</
a
>

<
a
href
=
"
http://www.dreamdu.com/exe/first/3.html
"
>
链接到3.html
</
a
>

<
a
href
=
"
http://www.dreamdu.com/exe/first/second/4.html
"
>
链接到4.html
</
a
>

本例中:

<form action="http://localhost:8080/cookieAndsession/showItems">
new Item order:<input type="text" value="yache"/><br>
<input type="submit" value="show all purched">
</form>提交后URL显示http://localhost:8080/cookieAndsession/showItems4.2使用相对路径:(相对于本文件的路径)   

如表示同级目录的文件

2.html和3.html在同一个文件夹下, 如果2.html链接到3.html,可以在2.html中这样写:
<
a
href
=
"
3.html
"
>
同目录下文件间互相链接
</
a
>
本例中中可以使用:
  

<form action="../cookieAndsession/showItems">//html的上一层目录(../)是根目录/所以还需指定web应用和servlet
new Item order:<input type="text" value="yache"/><br>
<input type="submit" value="show all purched">
</form>或者:<form action="./showItems">//文件的当前目录是cookieAndsession。servlet是当前目录下的showItems文件
new Item order:<input type="text" value="yache"/><br>
<input type="submit" value="show all purched">
</form>提交后URL显示http://localhost:8080/cookieAndsession/showItems。若改为:<form action="cookieAndsession/showItems">
new Item order:<input type="text" value="yache"/><br>
<input type="submit" value="show all purched">
</form>提交后URL显示:http://localhost:8080/cookieAndsession/cookieAndsession/showItems从而出现路径错误

4.3 根目录(一般以/开头)

  使用根目录的方式表示的路径和绝对路径的表示方式相似,去掉前面的域名就可。  比如:
  <
a
href
=
"
/exe/1.html
"
>
链接到1.html
</
a
>

  
<
a
href
=
"
/exe/first/2.html
"
>
链接到2.html
</
a
>

    本例:<form action="/cookieAndsession/showItems">
new Item order:<input type="text" value="yache"/><br>
<input type="submit" value="show all purched">
</form>
</center>  点击提交后URL:http://localhost:8080/cookieAndsession/showItems。总结:  不管使用绝对路径、相对路径还是根目录;都可以将html提交到相应的servlet。绝对路径是写的完整路径。但在不同的开发部署中移到另一计算机可能会出现错误(开发和部署一般不再同一台计算机,必须对表单进行修改),而使用相对路径是把先对与本文件URL;根目录的方式表示的路径和绝对路径的表示方式相似,[b]去掉前面的域名就可。使用相对路径和根目录有更好的移植特性。[/b] 推荐使用根目录(不管html放在哪个目录下都可以找到对应的servlet)

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: