首先登录的html页面
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>from</title> </head> <body> <form action="/Hello" method="GET"> 网址名:<input type="text" name="name"> <br /> 网址:<input type="text" name="url" /> <input type="submit" value="提交" /> </form> <a href="/info">info</a><br/> <a href="/Login.html">登陆</a> </body> </html>
javaServlet
package com.niefengjun.cn;
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 {
/**
* 登陆
* @param req
* @param res
* @throws ServletException
* @throws IOException
*/
@Override
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
res.setContentType("text/html;charset=gb22312");
String name = req.getParameter("user");
String pwd = req.getParameter("password");
if (name != null && pwd != null && name.equals("niefengjun") && pwd.equals("1234")) {
res.sendRedirect("success.html");
} else {
res.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "服务器忙,请稍后登陆");
}
}
@Override
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
{
doGet(req, res);
}
}
}webxml <servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>com.niefengjun.cn.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping> 运行结果

相关代码 https://gitee.com/niefengjun/javaServlet/tree/master
你打算打赏多少钱呢?
(微信扫一扫)