今天给大家分享一下踩坑记录。
今天准备用Servlet重新开发一个web程序。
开始是通过实现Servlet今天实现的。
@WebServlet("/demo1")
public class ServletDemo1 implements Servlet {
public void init(ServletConfig servletConfig) throws ServletException {
System.out.println("init");
}
public ServletConfig getServletConfig() {
return null;
}
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("service");
servletResponse.getWriter().write("hello servlet");
}
public String getServletInfo() {
return null;
}
public void destroy() {
System.out.println("destroy");
}
}
后台发现还有更好的实现方法通过继承HttpServlet.
@WebServlet("/demo3")
public class ServletDemo3 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Map<String, String[]> param = req.getParameterMap();
for (String s : param.keySet()) {
String[] strings = param.get(s);
for (String string : strings) {
System.out.println(s+":"+string);
}
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Map<String, String[]> param = req.getParameterMap();
for (String s : param.keySet()) {
String[] strings = param.get(s);
for (String string : strings) {
System.out.println(s+":"+string);
}
}
resp.setContentType("text/html;charset=utf-8");
resp.getWriter().write("nihao");
}
}
并能更好的区分 方法是GET 还是POST等。让代码符合RESF 的风格。
具体有那些方法接口如下
标签:JAVA,String,void,技术,param,System,println,SERVLET,public From: https://blog.51cto.com/u_16367561/8504342