Form表单中的action路径问题,form表单action路径《jsp--->Servlet路劲问题》这个和上一个《jsp--->Servlet》文章有关

2023-05-16,,

Form表单中的action路径问题,form表单action路径

热度5 评论 50

www.BkJia.Com  网友分享于:  2014-08-14 08:08:01     浏览数44525次

 

Form表单中的action路径问题,form表单action路径

今天刚接触web,在用jsp和servlet做一个简单的登陆的时候在Form表单action属性和method属性的一些问题;

我遇到的是Form表单提交到servelet处理时遇到的问题: 
(1)<form name="login" action="①?" method=“②?”>

//表单内容

username:<input type = "text" name = "username"> <br>
password:<input type = "password" name = "pwd"> <br>
<input type = "submit">
</form>

(2)对应的处理用户请求的servlet类为HelloServlet.java;

(3)配置web.xml文件: 
<servlet> 
<servlet-name>③servlet</servlet-name> 
<servlet-class>/HelloServlet</servlet-class> 
</servlet>

<servlet-mapping> 
<servlet-name>③servlet</servlet-name> 
<url-pattern>/①welcome</url-pattern> 
</servlet-mapping>

然后在login.jsp中应该是:

<form action="①welcome" method="②?">

这样的话login.jsp的url是http://localhost:8080/jsp/login.jsp

而HelloServlet.java的url是http://localhost:8080/jsp/welcome

在HelloServlet中可以取值

String name = "";
String pwd = "";
name = req.getParameter("username");
pwd = req.getParameter("pwd");

注:web.xml中③对应的两个servlet-name要一致;①中的url-pattern要与form表单中的action属性值一致 ;③ method方法默认是get方法,但是这种方法会将值暴露在浏览器上,所以一般使用的是post方法,隐藏值内容;

对于java中form表单action路径问题

 

一般action的路径配置你应该在struts.xml中给package加一个namespace,
然后让namespace的值和jsp文件夹的名字一致。
那么在写form 的action路径的时候,你就可以直接写相对路径了。例如
<form method="post" action="add_Emp.action">
而看到楼主action的请求有_ 应该是在struts.xml的action 中用了通配符吧
<action name="add_* " class="略" method="{1}">这样就是配置的通常用法
希望楼主能用的开心

在action中怎得到form表单file类型的文件路径

 

<form action="" method="post">
<input type="file" name="file1"/>
</form>
request.getParameter("file1");

<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="file1"/>
</form>
ServletInputStream sis = request.getInputStream();
用sis读取,一行一行分析.

Form表单中的action路径问题,form表单action路径《jsp--->Servlet路劲问题》这个和上一个《jsp--->Servlet》文章有关的相关教程结束。

《Form表单中的action路径问题,form表单action路径《jsp--->Servlet路劲问题》这个和上一个《jsp--->Servlet》文章有关.doc》

下载本文的Word格式文档,以方便收藏与打印。