写一个登录小程序,Servlet 和 HttpServlet 比较

详见
https://wjftu.com/zh/tuts/javaweb/servlet/

在 web.xml 里配置全局变量

	<context-param>
		<param-name>usernameparam-name>
		<param-value>wjfparam-value>
	context-param>
	<context-param>
		<param-name>passwordparam-name>
		<param-value>123456param-value>
	context-param>

写一个登录界面


<html>
<head>
<meta charset="UTF-8">
<title>Insert title heretitle>
head>
<body>
	<form action="loginservlet" method="post">
		user:<input type="text" name="username"/>
		password:<input type="password" name="password"/>
		<input type="submit" value="Submit"/>
	form>
body>
html>

实现 Servlet 接口
代码很复杂

package com.wjftu.javaweb;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class LoginServlet2 implements Servlet {

	@Override
	public void destroy() {

	}

	@Override
	public ServletConfig getServletConfig() {
		return null;
	}

	@Override
	public String getServletInfo() {
		return null;
	}
	
	private ServletContext servletContext;

	@Override
	public void init(ServletConfig servletConfig) throws ServletException {
		this.servletContext=servletConfig.getServletContext();
	}

	@Override
	public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String initUsername = servletContext.getInitParameter("username");
		String initPassword = servletContext.getInitParameter("password");
		
		PrintWriter out = response.getWriter();
		
		if(initUsername.equals(username) && initPassword.equals(password)) {
			out.println("Hello "+username);
		} else {
			out.println("wrong password");
		}
	}

}

继承父类 HttpServlet
简单多了

package com.wjftu.javaweb;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

    public LoginServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		String initUsername = getServletContext().getInitParameter("username");
		String initPassword = getServletContext().getInitParameter("password");
		
		System.out.println(initUsername+initPassword);
		
		PrintWriter out = response.getWriter();
		if(initUsername.equals(username)&&initPassword.equals(password)) {
			out.println("hello "+username);
		}else {
			out.println("wrong password");
		}
	}
}

你可能感兴趣的:(Java,servlet)