Question about the action path in Form, form Form action path
Today, I just got into touch with some questions about the Form action attributes and method attributes when using jsp and servlet for a simple login;
What I encountered was a problem when the Form was submitted to servelet for processing:
(1) <form name = "login" action = "①? "Method =" ②? ">
// Form Content
Username: <input type = "text" name = "username"> <br>
Password: <input type = "password" name = "pwd"> <br>
<Input type = "submit">
</Form>
(2) the corresponding servlet class for processing user requests is HelloServlet. java;
(3) configure the web. xml file:
<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>
In login. jsp, it should be:
<Form action = "① welcome" method = "②? ">
In this case, the url of login. jsp is http: // localhost: 8080/jsp/login. jsp.
The HelloServlet. java url is http: // localhost: 8080/jsp/welcome.
Available values in HelloServlet
String name = "";
String pwd = "";
Name = req. getParameter ("username ");
Pwd = req. getParameter ("pwd ");
Note: web. in xml, ③ the two servlet-names must be consistent; In ①, url-pattern must be consistent with the action attribute value in form; ③ The method is the get method by default, however, this method exposes the value to the browser, so the post method is generally used to hide the value content;
Question about the form action path in java
Generally, you should add a namespace to the package in struts. xml to configure the action path,
Then, make the namespace value consistent with the jsp folder name.
When writing the form action path, you can directly write the relative path. For example
<Form method = "post" action = "add_Emp.action">
As you can see, _ in the request of the landlord action should use wildcards in the action of struts. xml.
<Action name = "add _ *" class = "slightly" method = "{1}"> This is the common configuration usage.
Hope the landlord can use it happily
How to obtain the file path of form file in action?
<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 ();
Read with sis, one row for analysis.