servlet
log4j2
2016年5月24日
16:12
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<File name="log" fileName="logs.log" append="false">
<PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
</File>
</Appenders>
<Loggers>
<Logger name="com.dbyl.libarary.utils.Log" level="all">
<AppenderRef ref="Console"/>
<AppenderRef ref="log"/>
</Logger>
<Root level="trace">
<AppenderRef ref="Console"/>
<AppenderRef ref="log"/>
</Root>
</Loggers>
</Configuration>
@WebServlet(urlPatterns = "/HomeServlet",asyncSupported=true)
public class HomeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final Logger LOGGER = LogManager.getLogger(HomeServlet.class);
public static void main(String[] args) {
LOGGER.info("--------------info------------------"+HomeServlet.class);
}
public void testLog() {
LOGGER.trace("Trace Message!");
LOGGER.debug("Debug Message!");
LOGGER.info("Info Message!");
LOGGER.warn("Warn Message!");
LOGGER.error("Error Message!");
LOGGER.fatal("Fatal Message!");
LOGGER.info("--------------info------------------"+HomeServlet.class.getName());
LOGGER.info("--------------info------------------"+HomeServlet.class);
LOGGER.debug("------------debug--------------------");
LOGGER.fatal("---------------fatal-----------------");
LOGGER.info("--------------------------------");
getLogger(Bar.class.getName())
getLogger(MyAPP.class) //官方推荐的写法
getLogger("HomeServlet")
getLogger()
public class MyService {
// instead of Logger logger = LogManager.getLogger(MyService.class):
private static final ExtLogger logger = ExtLogger.create(MyService.class);
public void demoExtendedLogger() {
// ...
logger.trace("the built-in TRACE level");
logger.verbose("a custom level: a VERBOSE message");
logger.debug("the built-in DEBUG level");
logger.notice("a custom level: a NOTICE message");
logger.info("the built-in INFO level");
logger.diag("a custom level: a DIAG message");
logger.warn("the built-in WARN level");
logger.error("the built-in ERROR level");
logger.fatal("the built-in FATAL level");
logger.notice("java 8 lambda expression only executed if NOTICE is enabled: {}", () -> someMethod());
// ...
}
...
}
17.1.7 Generating Extende
2016年5月30日
10:09
servlet3.1上传图片
package cn.endianmeili.servlet;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* Servlet implementation class UserServlet
*/
@WebServlet(asyncSupported = true, urlPatterns = { "/UserServlet" })
@MultipartConfig//标识Servlet支持文件上传
public class UserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final Logger LOGGER = LogManager.getLogger(UserServlet.class);
/**
* @see HttpServlet#HttpServlet()
*/
public UserServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* 根据请求头解析出文件名
* 请求头的格式:火狐和google浏览器下:form-data; name="file"; filename="snmp4j--api.zip"
* IE浏览器下:form-data; name="file"; filename="E:\snmp4j--api.zip"
* @param header 请求头
* @return 文件名
*/
public String getFileName(String header) {
/**
* String[] tempArr1 = header.split(";");代码执行完之后,在不同的浏览器下,tempArr1数组里面的内容稍有区别
* 火狐或者google浏览器下:tempArr1={form-data,name="file",filename="snmp4j--api.zip"}
* IE浏览器下:tempArr1={form-data,name="file",filename="E:\snmp4j--api.zip"}
*/
String[] tempArr1 = header.split(";");
/**
*火狐或者google浏览器下:tempArr2={filename,"snmp4j--api.zip"}
*IE浏览器下:tempArr2={filename,"E:\snmp4j--api.zip"}
*/
String[] tempArr2 = tempArr1[2].split("=");
//获取文件名,兼容各种浏览器的写法
String fileName = tempArr2[1].substring(tempArr2[1].lastIndexOf("\\")+1).replaceAll("\"", "");
return fileName;
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//存储路径
String savePath = request.getServletContext().getRealPath("/uploadFile");
//获取上传的文件集合
Collection<Part> parts = request.getParts();
//上传单个文件
if (parts.size()==1) {
//Servlet3.0将multipart/form-data的POST请求封装成Part,通过Part对上传的文件进行操作。
//Part part = parts[0];//从上传的文件集合中获取Part对象
Part part = request.getPart("upload");//通过表单file控件(<input type="file" name="file">)的名字直接获取Part对象
//Servlet3没有提供直接获取文件名的方法,需要从请求头中解析出来
//获取请求头,请求头的格式:form-data; name="file"; filename="snmp4j--api.zip"
String header = part.getHeader("content-disposition");
//获取文件名
String fileName = getFileName(header);
//把文件写到指定路径
String imgtype = fileName.substring(fileName.lastIndexOf("."));
fileName = String.valueOf(Math.random()*10) + imgtype;
String ctxPath = savePath+File.separator+fileName;
part.write(ctxPath);
String URL = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
String localhostUrl = "uploadFile/";
String callbackUrl = URL + localhostUrl + fileName;
String callback = request.getParameter("CKEditorFuncNum");
PrintWriter out = response.getWriter();
out.println("<script type=\"text/javascript\">");
out.println("window.parent.CKEDITOR.tools.callFunction(" + callback + ",'" + callbackUrl + "','')");
out.println("</script>");
}
// String username = request.getParameter("username");
// String password = request.getParameter("password");
//
// String result = "用户名或密码不正确";
// if (username!=null&&password!=null) {
// if ("hello".equals(username)&&"app01".equals(password)) {
// result = "登录成功";
// }
// }
//
// response.getWriter().append(result).append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
servlet3.1上传图片
2016年5月14日
12:14
package cn.endianmeili.servlet;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* Servlet implementation class UserServlet
*/
@WebServlet(asyncSupported = true, urlPatterns = { "/UserServlet" })
@MultipartConfig//标识Servlet支持文件上传
public class UserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final Logger LOGGER = LogManager.getLogger(UserServlet.class);
/**
* @see HttpServlet#HttpServlet()
*/
public UserServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* 根据请求头解析出文件名
* 请求头的格式:火狐和google浏览器下:form-data; name="file"; filename="snmp4j--api.zip"
* IE浏览器下:form-data; name="file"; filename="E:\snmp4j--api.zip"
* @param header 请求头
* @return 文件名
*/
public String getFileName(String header) {
/**
* String[] tempArr1 = header.split(";");代码执行完之后,在不同的浏览器下,tempArr1数组里面的内容稍有区别
* 火狐或者google浏览器下:tempArr1={form-data,name="file",filename="snmp4j--api.zip"}
* IE浏览器下:tempArr1={form-data,name="file",filename="E:\snmp4j--api.zip"}
*/
String[] tempArr1 = header.split(";");
/**
*火狐或者google浏览器下:tempArr2={filename,"snmp4j--api.zip"}
*IE浏览器下:tempArr2={filename,"E:\snmp4j--api.zip"}
*/
String[] tempArr2 = tempArr1[2].split("=");
//获取文件名,兼容各种浏览器的写法
String fileName = tempArr2[1].substring(tempArr2[1].lastIndexOf("\\")+1).replaceAll("\"", "");
return fileName;
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//存储路径
String savePath = request.getServletContext().getRealPath("/uploadFile");
//获取上传的文件集合
Collection<Part> parts = request.getParts();
//上传单个文件
if (parts.size()==1) {
//Servlet3.0将multipart/form-data的POST请求封装成Part,通过Part对上传的文件进行操作。
//Part part = parts[0];//从上传的文件集合中获取Part对象
Part part = request.getPart("upload");//通过表单file控件(<input type="file" name="file">)的名字直接获取Part对象
//Servlet3没有提供直接获取文件名的方法,需要从请求头中解析出来
//获取请求头,请求头的格式:form-data; name="file"; filename="snmp4j--api.zip"
String header = part.getHeader("content-disposition");
//获取文件名
String fileName = getFileName(header);
//把文件写到指定路径
String imgtype = fileName.substring(fileName.lastIndexOf("."));
fileName = String.valueOf(Math.random()*10) + imgtype;
String ctxPath = savePath+File.separator+fileName;
part.write(ctxPath);
String URL = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
String localhostUrl = "uploadFile/";
String callbackUrl = URL + localhostUrl + fileName;
String callback = request.getParameter("CKEditorFuncNum");
PrintWriter out = response.getWriter();
out.println("<script type=\"text/javascript\">");
out.println("window.parent.CKEDITOR.tools.callFunction(" + callback + ",'" + callbackUrl + "','')");
out.println("</script>");
}
// String username = request.getParameter("username");
// String password = request.getParameter("password");
//
// String result = "用户名或密码不正确";
// if (username!=null&&password!=null) {
// if ("hello".equals(username)&&"app01".equals(password)) {
// result = "登录成功";
// }
// }
//
// response.getWriter().append(result).append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
2016年5月30日
10:56
package dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class BaseDao {
private String driver = "oracle.jdbc.driver.OracleDriver";
private String url = "jdbc:oracle:thin:@localhost:1521:orcl";
private String username = "111";
private String userpwd = "111";
private PreparedStatement pStatement;
private Connection connection;
private ResultSet rSet;
public void getConn() {
try {
Class.forName(driver);
connection = DriverManager.getConnection(url, username, userpwd);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public ResultSet resultSet(String sql, String[] values) {
getConn();
try {
pStatement = connection.prepareStatement(sql);
if (values != null) {
for (int i = 0; i < values.length; i++) {
pStatement.setString(i + 1, values[i]);
}
}
rSet = pStatement.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rSet;
}
public int updateSql(String sql, String[] values) {
getConn();
int rows = 0;
try {
pStatement = connection.prepareStatement(sql);
if (values != null) {
for (int i = 0; i < values.length; i++) {
pStatement.setString(i + 1, values[i]);
}
}
rows = pStatement.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rows;
}
public void coloseAll() {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
已使用 Microsoft OneNote 2016 创建。