Open saltfish666 opened 6 years ago
想要编译一个 servlet 只能
javac -sourcepath /usr/local/apache-tomcat-8.5.29/webapps/examples/WEB-INF/classes:. -cp /usr/local/apache-tomcat-8.5.29/lib/servlet-api.jar Saltfish.java
前一个目录寻找util 包,后者寻找 servlrt 包
这么长就这么长吧
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Enumeration;
import java.util.ResourceBundle;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import util.HTMLFilter;
public class Saltfish extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final ResourceBundle RB = ResourceBundle.getBundle("LocalStrings");
@Override
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html><html>");
out.println("<head>");
out.println("<meta charset=\"UTF-8\" />");
String title = RB.getString("sessions.title");
out.println("<title>" + title + "</title>");
out.println("</head>");
out.println("<body bgcolor=\"white\">");
out.println("<h1>hi</h1>");
out.println("</body>");
out.println("</html>");
}
@Override
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
doGet(request, response);
}
}
继承HttpServlet,重写doGet,写入文本,编译成class,注册到web.xml
![][https://imgur.com/J3Hl8Mj]
这算是所谓的约定吧,痛恨。。。
实现servlet规范,编译成class文件,注册在web.xml中