创建工程
搭建Tomcat
项目创建
编码
HelloSerclet
package com.zr.servlet;
import javax.servlet.*;
import java.io.IOException;
public class HelloServlet implements Servlet {
@Override
public void init(ServletConfig servletConfig) throws ServletException {
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
servletResponse.getWriter().write("Hello");
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}
LoginSerclet
package com.zr.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class LoginServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username=req.getParameter("username");
String password=req.getParameter("password");
if (username.equals("123")&&password.equals("123")){
resp.getWriter().write("success");
}else {
resp.getWriter().write("failure");
}
}
}
index
<html>
<head>
<title>$Title$</title>
</head>
<body>
<form action="/login" method="post">
username:<input name="username" type="text">
password:<input name="password" type="password">
<input type="submit" value="login">
</form>
</body>
</html>
Web
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>com.zr.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.zr.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>