Servlet实例流程:
login.jsp-LoginServlet-打印登陆信息
第一步:新建LoginServlet.java文件
package com.jikexueyuan.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public classLoginServlet extendsHttpServlet {
//点击 public class LoginServlet extends HttpServlet左边的警告 按钮 点击Add generated serial version ID 生成以下一行代码:序列号版本ID,用于序列化和反序列化
/**
*
*/
privatestatic finallong serialVersionUID =6520550639750164887L;
@Override
protectedvoid service(HttpServletRequestreq, HttpServletResponse resp)throws ServletException,IOException {
String userName= req.getParameter("uname");
String password= req.getParameter("upwd");
System.out.println("用户名 ==》 "+ userName);
System.out.println("密码 ==》 "+ password);
}
}
第二步: WebContent 文件夹下新建14文件夹:新建login.jsp文件
<%@ pagelanguage="java"contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE htmlPUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"content="text/html; charset=UTF-8">
<title>登录界面</title>
</head>
<body>
<!-- request.getContextPath() 应用的上下文路径http://localhost:8080/JSPStudy -->
<form action="<%= request.getContextPath()%>/loginServlet"method="post">
userName:<input type="text"name="uname" /> <br/>
password:<input type="password"name="upwd" /> <br/>
<input type="submit"name="Login" />
<input type="reset"name="Reset" />
</form>
</body>
</html>
第三步:打开WEB-INF下的web.xml添加以下代码
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.jikexueyuan.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
第五步:打开浏览器 http://localhost:8080/JSPStudy/14/login.jsp
输入用户名developer和密码111111 点击“提交”按钮
eclipse控制台会输出形如
用户名 ==》 developer
密码 ==》 111111