`
mj4d
  • 浏览: 302624 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

some tips

    博客分类:
  • java
阅读更多

工作中遇到的一些问题,有些解决方案来自网络,Mark之,持续更新

  • HttpURLConnection的403

用HttpURLConnection去读取某个网页的内容时,老是会发生这个403错误,而直接用firefox访问这个网站时就没问题。

写道
403 Forbidden 对被请求页面的访问被禁止。

 java代码HttpURLConnection去连的话 http header 中的User-Agent就为空,解决方法就是在连接之前先设置这个属性:

        URL url = new URL("");
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setRequestProperty("User-agent","Mozilla/4.0");
        conn.setRequestMethod("POST");
        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
 
  • 可执行的jar

进入class目录,这里为bin目录:

D:\workspace\workspace\test\bin>jar cvfe test.jar com.sample.Sample .
 

 这边表示打包后的jar文件名为test.jar,该jar包的主方法为com.sample.Sample,当然Sample.java中需要有main方法:

package com.sample;
public class Sample {
    
    public static void main(String[] args) {
        System.out.println(System.getProperty("java.version"));
    }

}

 将会在test.jar的META-INF/MANIFEST.MF中看到

Manifest-Version: 1.0
Created-By: 1.6.0_25 (Sun Microsystems Inc.)
Main-Class: com.sample.Sample

 由于已经位于bin目录,这里将当前目录(.)作为打包的内容。
运行该jar

D:\workspace\workspace\test\bin>java -jar test.jar
1.6.0_25

 

  • servlet读取spring注入对象

       考虑某些场景,比如某个请求必须要servlet访问。但是该servlet中有其他资源,如datasource,但是用spring配置集成,现在问题就是需要在servlet中访问spring依赖注入的资源。需要做的就是将web容器实例化的servlet对象绑定到spring容器的context上下文中 ,这样就可以在servlet中去访问spring容器注入的对象。

在spring中提供了相应的绑定方法:

WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc)

 其他该怎么配置还怎么配置,比如web.xml中:

	<!-- spring config -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring/spring-*.xml</param-value>
	</context-param>
	
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- target servlet -->
	<servlet>
		<servlet-name>test-user</servlet-name>
		<servlet-class>com.sample.tec.sales.web.UserDaoServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>test-user</servlet-name>
		<url-pattern>/test-user</url-pattern>
	</servlet-mapping>

  其他在spring配置文件注入接口就不再赘述,见UserDaoServlet中的

private LoginService      userService;
...............

    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println(UserDaoServlet.class.getName() + ".ROOT");
        WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        userService = (LoginService) ctx.getBean("userService");
        userService.foo();
    }

 

 

  • Could not find artifact com.sun:tools:jar:1.5.0

把eclipse中的【Windows】-【Preferences】-【Java】-【Install JREs】改为JDK,而不用JRE如果还不行就把jre删除

 

分享到:
评论

相关推荐

    SomeTips:多重血条,粒子特效置顶,BundleManger

    在Unity游戏开发中,"SomeTips:多重血条,粒子特效置顶,BundleManger"这一主题涵盖了几个关键的技术点,这些技术对于提高游戏的视觉效果和性能优化至关重要。下面将详细阐述这些知识点: 1. **多重血条**:在许多...

    c++ standard library practical tips

    Thus, for example, if you’re about to start working with vectors and would like some background first, you can leaf through Chapter 4, “Tips on Vectors.” It will give you some tips on the power ...

    Some tips of wmi scripting in jscript (1)

    在Windows脚本中,WMI(Windows Management Instrumentation)是一个强大的工具,用于管理和监控系统信息。JScript,作为Microsoft的脚本语言,同样可以利用WMI进行自动化任务。本文主要探讨的是在JScript中如何有效...

    What Context.pdf

    介绍context 的作用 ...I’d like to do is provide for you some insights on how Context works alongside some tips that will (hopefully) allow you to leverage it more effectively in your applications.

    tips on WCDMA measurement

    some tips on 3G wcdma measurement

    如何编写批处理文件批处理文件批处理文件

    简明批处理教程22009年10月20日 星期二 下午 05:35 最近对于批处理技术的探讨比较热,也有不少好的批处理程序发布,但是如果没有一定的相关知识恐怕不容易看懂和理解这些批处理文件,也就更谈不上自己动手编写了,古...

    tips and tricks ultimate

    Some tips and tricks about Windows Ultimate.

    professor Stewart‘s cabinet of mathematical curiosities

    geometry and probability -- like how to extract a cherry from a cocktail glass (harder than you think), a pop up dodecahedron, the real reason why you can't divide anything by zero and some tips for ...

    Delphi简单的FTP服务器

    简单的FTP服务器 ... FTP Server By: Bryan Cairns ...If youre a Dephi Pro, you can probably give me some tips as I am still new to Delphi programming. Bryan Cairns cairnsb@ameritech.net

    Selenium Testing Tools Cookbook(PACKT,2ed,2015)

    Finally, we give you some tips on integrating Selenium WebDriver with other popular tools and testing mobile applications. By the end of this book, you will have learned enough to solve complex ...

    Mastering JavaScript Object-Oriented Programming

    We'll use the famous promises to work with asynchronous processes and will give you some tips on how to organize your code effectively. You'll find out how to create robust code using SOLID ...

    #GDC2018 Ray Tracing in Games with NVIDIA RTX - GDC 2018.pdf

    Finally NVIDIA will also provide some tips and guideline for integrating DXR into your engine, and discuss open challenges. Speakers: Ignacio Llamas, Senior Manager, Real Time Rendering Software &...

    Ajax on Rails

    For those new to Rails, this book provides a quick introduction, the big picture, a walk through the installation process, and some tips on getting started. If you've already started working with ...

    Here are some common interview questions for a software engineer

    Here are some common interview questions for a software engineer position, along with some tips on how to answer them: Programming questions What is your favorite programming language and why? Tip: ...

    Web Microanalysis of Big Image Data.pdf

    This book looks at the increasing interest in running microscopy processing algorithms on big image ... Some tips are provided as practical suggestions to improve accuracy or computational performance.

    Machine Learning Projects with TensorFlow 2.0:Supercharge your Machine Learning

    Each project will put your skills to test, help you understand and overcome the challenges you can face in a real-world scenario and provide some tips and tricks to help you become more efficient....

    批处理之组合命令.pdf

    如果 `sometips.gif` 文件存在,将会列出该文件信息。 5. Windows 注册表操作 通过批处理脚本,可以使用 `REG` 命令来操作Windows注册表。它可以添加、修改、删除注册表项或值。文档中提到了使用 `REG ADD`、`REG ...

    Selenium.Testing.Tools.Cookbook.2nd.Edition.178439251

    Finally, we give you some tips on integrating Selenium WebDriver with other popular tools and testing mobile applications. By the end of this book, you will have learned enough to solve complex ...

    mac php zip 扩展

    In order to make phpize and php-config find the PHP header files in the location that XCode places them rather than in /usr/include/php -- a read-only location now -- I followed some tips I found ...

Global site tag (gtag.js) - Google Analytics