servlet的基本使用
package com.xxx.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(name="Servlet01",urlPatterns={"/s01","/s001"})
public class servlet01 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("hello servlet");
String URL = req.getRequestURL()+"";
System.out.println("获取请求的完整路径:"+URL);
String uri = req.getRequestURI();
System.out.println("获取请求的部分路径:"+uri);
String queryString = req.getQueryString();
System.out.println("获取请求时的参数字符串:"+queryString);
String method = req.getMethod();
System.out.println("获取请求的方法:"+method);
String prototol = req.getProtocol();
System.out.println("获取请求的协议版本:"+prototol);
String webapp = req.getContextPath();
System.out.println("获取项目的站点名:"+webapp);
String uname = req.getParameter("uname");
String upwd = req.getParameter("upwd");
System.out.println("uname:"+uname);
System.out.println("upwd:"+upwd);
String[] hobbys = req.getParameterValues("hobby");
if(hobbys!=null&&hobbys.length>0){
for(String hobby:hobbys){
System.out.println("爱好:"+hobby);
}
}
};
}
servlet请求转发
package com.xxx.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/s02")
public class servlet02 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String uname = req.getParameter("uname");
System.out.println("servlet02 uname:"+uname);
req.getRequestDispatcher("s03").forward(req,resp);
};
}
/s03
package com.xxx.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/s03")
public class servlet03 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String uname = req.getParameter("uname");
System.out.println("servlet03 uname:"+uname);
};
}
servlet转发自定参数
package com.xxx.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/s04")
public class servlet04 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("servlet04");
req.setAttribute("name","admin");
req.setAttribute("age",18);
List<String> list = new ArrayList<>();
list.add("aaa");
list.add("bbb");
req.setAttribute("list",list);
req.getRequestDispatcher("s05").forward(req,resp);
};
}
/s05
package com.xxx.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/s05")
public class servlet05 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("servlet05");
String name = (String) req.getAttribute("name");
System.out.println("name:"+name);
Integer age = (Integer) req.getAttribute("age");
System.out.println("age:"+age);
List<String> list = (List<String>) req.getAttribute("list");
System.out.println("list:"+list.get(0));
};
}
servlet设置服务端和客户端编码
package com.xxx.servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
@WebServlet("/s06")
public class servlet06 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=UTF-8");
PrintWriter writer = resp.getWriter();
writer.write("<h2>你好</h2>");
};
}
servlet重定向
package com.xxx.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet("/s07")
public class servlet07 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("servlet07");
resp.sendRedirect("s08");
};
}
servlet+filter+jdbc做一个简单的后端接口
设置一个公共可用的jdbc接口
package com.xxx.utils;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class jdbc {
private static String driver = null;
private static String url = null;
private static String username = null;
private static String password = null;
static {
try{
driver = "com.mysql.cj.jdbc.Driver";
url = "jdbc:mysql://localhost:3306/chinaschool?serverTimezone=UTC";
username = "root";
password = "123456";
Class.forName(driver);
}catch (Exception e){
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url,username,password);
}
public static void release(Connection c, Statement s, ResultSet r) throws SQLException {
if(r!=null){
r.close();
}
if(s!=null){
s.close();
}
if(c!=null){
c.close();
}
}
}
filter
package com.xxx.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebFilter("/test02")
public class Filter02 implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("Filter01 init...");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("doFilter");
HttpServletResponse resp = (HttpServletResponse) servletResponse;
resp.setHeader("Access-Control-Allow-Origin", "*");
filterChain.doFilter(servletRequest,resp);
}
@Override
public void destroy() {
}
}
servlet
package com.xxx.test;
import com.xxx.utils.jdbc;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
@WebServlet("/test02")
public class test02 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("application/json");
resp.setCharacterEncoding("utf-8");
Connection conn = null;
PreparedStatement st = null;
ResultSet rs = null;
try {
conn = jdbc.getConnection();
System.out.println(conn);
String sql = "select sub.subject,sub.station_name from subjects as sub where sub.subject = ?";
st = conn.prepareStatement(sql);
st.setString(1,"化学");
rs = st.executeQuery();
JSONArray result = resultToJsonArray(rs);
PrintWriter out = resp.getWriter();
out.println(result);
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
jdbc.release(conn,st,rs);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static JSONArray resultToJsonArray(ResultSet rs) throws SQLException {
JSONArray array = new JSONArray();
ResultSetMetaData metaData = rs.getMetaData();
int column = metaData.getColumnCount();
while (rs.next()){
JSONObject jsonObj = new JSONObject();
for(int i = 1;i<=column;i++){
String columnName = metaData.getColumnLabel(i);
String value = rs.getString(columnName);
jsonObj.put(columnName,value);
}
array.put(jsonObj);
}
return array;
}
}
注意!servlet导入的jar包放在web/web-inf/lib下,并且不要用任何文件夹包含 jar包存放如下
|