[英]How do I write the Java logger Servlet?
I want to do some log in my system, like user action, and I know in the servelet I can get the request with all the session,parameter..etc我想在我的系统中进行一些登录,例如用户操作,并且我知道在小服务中我可以获得包含所有会话、参数等的请求
So I want to write the Servlet所以我想写Servlet
public class UserActionCheck extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
Map map = request.getParameterMap();
Set keSet = map.entrySet();
for (Iterator itr = keSet.iterator(); itr.hasNext(); ) {
Map.Entry me = (Map.Entry) itr.next();
Object ok = me.getKey();
Object ov = me.getValue();
String[] value = new String[1];
if (ov instanceof String[]) {
value = (String[]) ov;
} else {
value[0] = ov.toString();
}
for (int k = 0; k < value.length; k++) {
System.out.println(ok + "=" + value[k]);
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//HttpSession session = request.getSession();
}
}
then I can see the parameter output in the tomcat console..but I get the blank page..然后我可以在tomcat控制台中看到参数输出..但是我得到了空白页..
It seems the page is stop after doGet
method..似乎页面在doGet
方法之后停止了..
so how should I make it continue?那么我该如何让它继续下去呢?
use that RequestDispatcher
?使用那个RequestDispatcher
?
also how to handle in the doPost
?在doPost
还怎么处理?
For your purpose, the best way would be to use a Filter
.为了您的目的,最好的方法是使用Filter
。
Example :示例:
@WebFilter(filterName = "monitoringFilter", urlPatterns = { "/*" })
public class MonitoringFilter implements Filter
{
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
{
// Right here do your stuff pretty much like in a servlet
request // ... get information you need
// Process request as normal
chain.doFilter(request,response);
}
@Override
public void init(FilterConfig config) throws ServletException
{
}
@Override
public void destroy()
{
}
}
More info :更多信息:
You ask the question 你问这个问题
so how should I make it continue? 所以我应该如何继续下去?
The idea of the doGet method is that it should write to the response object, the html (or data) that should be written in the browser; doGet方法的想法是,它应该写入响应对象,即应该在浏览器中写入的html(或数据)。
For example you could write 例如你可以写
PrintWriter out = response.getWriter();
for(int k=0;k<value.length;k++){
out.println(ok+"="+value[k]);
}
Have a look at http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-First-Servlets.html 看看http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-First-Servlets.html
doPost can also be treated similarly but is usually used for POST'd data - it is called when a HTML form is submitted. doPost也可以类似地处理,但通常用于POST的数据-提交HTML表单时调用它。
You should use log4j and FileAppender to implement logging in your application.您应该使用log4j和FileAppender在您的应用程序中实现日志记录。 Something like this :::像这样的东西:::
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
class A{
static Log log = LogFactory.getLog(A.class);
void methodA(){
try{
log.info("I am inside A");
} catch(Exception e) {
log.error("error" , e);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.