今日学习成果
作者:互联网
创建servlet并配置web.xml
在src目录下新建Javaclass文件,命名为LoginServlet,继承HTTPServlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<welcome-file-list>
<welcome-file>/login.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
</web-app>
利用form表单提交信息
<form method="get" action="/loginServlet">
username:<input type="text" name="username"><br>
password:<input type="password" name="pwd"><br>
<button type="button" name="loginbutton" onclick="loginverify();">登 录</button>
<input type="submit">
</form>
在function中调取执行loginServlet
document.getElementById("form").submit();
在LoginServlet中利用submit重写doget方法,string声明username和password两个变量
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
System.out.println("name:" + username);
System.out.println("password:" + password);
}
}
在login中定义登录按钮链接到的路径path
<% String path = request.getContextPath(); %>
编译并发布后再次登录,可在后台看到输入的用户名与密码
标签:username,xml,req,成果,学习,org,今日,password,LoginServlet 来源: https://www.cnblogs.com/weishui/p/14005187.html