Java Code Examples for javax.servlet.http.HttpServletRequest.getServletContext()

The following are Jave code examples for showing how to use getServletContext() of the javax.servlet.http.HttpServletRequest class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: tomcat7   File: TestApplicationContext.java   View Source Code Vote up 7 votes
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    resp.setContentType("text/plain");
    PrintWriter pw = resp.getWriter();
    ServletContext sc = req.getServletContext();
    pw.println("01-" + sc.getContext("/bar").getInitParameter("id"));
    pw.println("02-" + sc.getContext("/foo").getInitParameter("id"));
    pw.println("03-" + sc.getContext("/foo##1").getInitParameter("id"));
    pw.println("04-" + sc.getContext("/foo##2").getInitParameter("id"));
    pw.println("05-" + sc.getContext("/foo##3").getInitParameter("id"));
    pw.println("06-" + sc.getContext("/unknown").getInitParameter("id"));
    pw.println("07-" + sc.getContext("/").getInitParameter("id"));
    pw.println("08-" + sc.getContext("/foo/bar").getInitParameter("id"));
    pw.println("09-" + sc.getContext("/football").getInitParameter("id"));
}
 
Example 2
Project: Servlet_Context_Interface   File: ContextServlet.java   View Source Code Vote up 6 votes
/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		response.setContentType("text/html");
		//get the response writer
		PrintWriter out = response.getWriter();
		//initialise the servlet context
		ServletContext context = request.getServletContext();
		//get the context name
		String lname = context.getServletContextName();
		//initialise the parameters
		String a = context.getInitParameter("a");
		String b = context.getInitParameter("b");
		String c = context.getInitParameter("c");
		String d = context.getInitParameter("d");
		
		Enumeration<String> e1 = context.getInitParameterNames();
		String param_Names = "";
		//appending attr names to a string variable 
		while(e1.hasMoreElements()) {
		param_Names = param_Names + e1.nextElement()+"<br>";
		}
				//set the attribute
		context.setAttribute("x", "XXX");
		context.setAttribute("y", "YYY");
		context.setAttribute("z", "ZZZ");

		String x = (String)context.getAttribute("x");
		String y = (String)context.getAttribute("y");
		String z = (String)context.getAttribute("z");
			//get the attribute names
				Enumeration<String> e2 = context.getAttributeNames();
					String attr_Names = "";
						//appending attr names to a string variable 
					while(e2.hasMoreElements()) {
						attr_Names = attr_Names + e2.nextElement()+"<br>";
					}//while close
//html code to display
		out.println("<html>");
			out.println("<body>");
				out.println("<h3>Servlet Context Details<h3>");
					out.println("<table border='1'>");
						out.println("<tr><td colspan='2'>Parameters Details</td></tr>");
							out.println("<tr><td>a</td><td>"+a+"</td></tr>");
							out.println("<tr><td>b</td><td>"+b+"</td></tr>");
							out.println("<tr><td>c</td><td>"+c+"</td></tr>");
							out.println("<tr><td>d</td><td>"+d+"</td></tr>");
								out.println("<tr><td>Parameter Names</td><td>"+param_Names+"</td></tr>");
									out.println("<tr><td colspan='2'>Attributes Details</td></tr>");
										out.println("<tr><td>x</td><td>"+x+"</td></tr>");
										out.println("<tr><td>y</td><td>"+y+"</td></tr>");
										out.println("<tr><td>z</td><td>"+z+"</td></tr>");
											out.println("<tr><td>Attributes Names</td><td>"+attr_Names+"</td></tr>");
												out.println("<tr><td>Logical Name</td><td>"+lname+"</td></tr>");
												out.println("<tr><td>Foreign Context</td><td>"+context.getContext("/app8")+"</td></tr>");
												out.println("</table></body></html>");

		}
 
Example 3
Project: apache-tomcat-7.0.73-with-comment   File: TestApplicationContext.java   View Source Code Vote up 6 votes
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    resp.setContentType("text/plain");
    PrintWriter pw = resp.getWriter();
    ServletContext sc = req.getServletContext();
    pw.println("01-" + sc.getContext("/bar").getInitParameter("id"));
    pw.println("02-" + sc.getContext("/foo").getInitParameter("id"));
    pw.println("03-" + sc.getContext("/foo##1").getInitParameter("id"));
    pw.println("04-" + sc.getContext("/foo##2").getInitParameter("id"));
    pw.println("05-" + sc.getContext("/foo##3").getInitParameter("id"));
    pw.println("06-" + sc.getContext("/unknown").getInitParameter("id"));
    pw.println("07-" + sc.getContext("/").getInitParameter("id"));
    pw.println("08-" + sc.getContext("/foo/bar").getInitParameter("id"));
    pw.println("09-" + sc.getContext("/football").getInitParameter("id"));
}
 
Example 4
Project: fpc   File: EventServer.java   View Source Code Vote up 6 votes
/**
 * Method for stream initialization
 * @param clientUri - Client Uri
 * @param request - The servlet request object
 * @param response - The servlet Response Object
 */
private void init(String clientUri,HttpServletRequest request, HttpServletResponse response){
	LOG.info("Response Stream Inititated");
	try{
		HttpSession session = request.getSession();
		session.setMaxInactiveInterval(72*60*60);
		EventClient client = new EventClient();
		client.connectToClient(clientUri);
		response.setHeader("Content-Type", "text/event-stream");
		response.setHeader("Cache-Control", "no-cache, no-store");
		response.setHeader("Connection", "keep-alive");
		AsyncContext asyncContext = request.startAsync(request,response);
		asyncContext.setTimeout(72*60*60*1000);
		asyncContext.getResponse().setBufferSize(1200);
		try {
			asyncContext.getResponse().flushBuffer();
		} catch (IOException e1) {
			ErrorLog.logError(e1.getMessage(),e1.getStackTrace());
		}
		ServletContext servletContext = request.getServletContext();
		Map<String,AsyncContext> responseStreams = (ConcurrentHashMap<String,AsyncContext>) servletContext.getAttribute("responseStreams");
		responseStreams.put(clientUri,asyncContext);
	} catch (Exception e){
		ErrorLog.logError(e.getMessage(),e.getStackTrace());
	}
}
 
Example 5
Project: TeamNote   File: NoteController.java   View Source Code Vote up 6 votes
@RequestMapping("/uploadNoteImage")
@ResponseBody
public void upladNoteImage(@RequestParam("upload") MultipartFile img, HttpServletRequest request, HttpServletResponse response)throws IOException{
    ServletContext servletContext = request.getServletContext();
    response.setContentType("text/html;charset=UTF-8");
    response.setHeader("X-Frame-Options", "SAMEORIGIN");
    PrintWriter out = response.getWriter();
    String CKEditorFuncNum = request.getParameter("CKEditorFuncNum");
    String imgName = Long.toString(System.currentTimeMillis()) + img.getOriginalFilename();
    String destPath = servletContext.getRealPath("/image/note/") + imgName;
    img.transferTo(new File(destPath));
    out.println("<script type=\"text/javascript\">");
    out.println("window.parent.CKEDITOR.tools.callFunction("
            + CKEditorFuncNum + ",'" + "/teamnote/image/note/" + imgName
            + "','')");
    out.println("</script>");
}
 
Example 6
Project: burpextender-proxyhistory-webui   File: JdbcConnectionInterceptor.java   View Source Code Vote up 5 votes
@Override
public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object handler) throws Exception {
    ServletWebRequest wr = new ServletWebRequest(req);
    ServletContext sctx = req.getServletContext();
    String dbname = (String) sctx.getAttribute("dbname");
    Connection dbconn = (Connection) wr.getAttribute("dbconn", WebRequest.SCOPE_REQUEST);
    if (Objects.isNull(dbconn) && !Strings.isEmpty(dbname)) {
        dbconn = DataStore.getConnection(dbname);
        wr.setAttribute("dbconn", dbconn, WebRequest.SCOPE_REQUEST);
    }
    return super.preHandle(req, res, handler);
}
 
Example 7
Project: solo-spring   File: ConsoleRenderer.java   View Source Code Vote up 5 votes
@Override
protected void beforeRender(final HttpServletRequest request, final HttpServletResponse response) throws Exception {
	ServletContext servletContext = request.getServletContext();
	if (ConsoleRenderer.servletContext == null) {
		ConsoleRenderer.servletContext = servletContext;
	}
}
 
Example 8
Project: TeamNote   File: UserController.java   View Source Code Vote up 5 votes
@RequestMapping("/uploadAvatar")
public String uploadavatar(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
    ServletContext servletContext = request.getServletContext();
    String avatarName = Long.toString(System.currentTimeMillis()) + file.getOriginalFilename();
    String destPath = servletContext.getRealPath("/image/avatar/") + avatarName;
    file.transferTo(new File(destPath));

    int userId = getUserId();

    userBasicService.updateavatar(userId, "/image/avatar/" + avatarName, servletContext.getRealPath("/"));
    return "redirect:/settings";
}
 
Example 9
Project: TeamNote   File: UserController.java   View Source Code Vote up 5 votes
@RequestMapping("/uploadQrcode")
public String uploadQrcode(@RequestParam("qrcode") MultipartFile qrcode, HttpServletRequest request) throws IOException {
    ServletContext servletContext = request.getServletContext();
    String qrcodeName = Long.toString(System.currentTimeMillis()) + qrcode.getOriginalFilename();
    String destPath = servletContext.getRealPath("/image/qrcode/") + qrcodeName;
    qrcode.transferTo(new File(destPath));

    int userId = getUserId();

    userBasicService.updateQrcode(userId, "/image/qrcode/" + qrcodeName, servletContext.getRealPath("/"));
    return "redirect:/settings";
}
 
Example 10
Project: TeamNote   File: UserController.java   View Source Code Vote up 5 votes
@RequestMapping("/deleteQrcode")
public String deleteQrcode(HttpServletRequest request) {
    ServletContext servletContext = request.getServletContext();
    int userId = getUserId();

    userBasicService.deleteQrcode(userId, servletContext.getRealPath("/"));
    return "redirect:/settings";
}