如何在JSP中获取由Servlet转发的请求URL?
如果我在JSP中运行以下代码,
System.out.println("servlet path= " + request.getServletPath());
System.out.println("request URL= " + request.getRequestURL());
System.out.println("request URI= " + request.getRequestURI());
Run Code Online (Sandbox Code Playgroud)
然后我得到JSP的服务器端路径.但是我想在浏览器的地址栏中看到URL.我可以在转发到JSP的Servlet中获取它,但我希望在JSP中获取它.
刚遇到奇怪的问题.当我打字
java -version
Run Code Online (Sandbox Code Playgroud)
我有
Error occurred during initialization of VM
Could not reserve enough space for object heap
Could not create the Java virtual machine.
Run Code Online (Sandbox Code Playgroud)
.
java -Xms64m -Xmx64m -version
Run Code Online (Sandbox Code Playgroud)
这个命令工作正常
java version "1.6.0_24"
Java(TM) SE Runtime Environment (build 1.6.0_24-b07)
Java HotSpot(TM) Client VM (build 19.1-b02, mixed mode)
Run Code Online (Sandbox Code Playgroud)
如果我将xms,xmx更改为128m,我会再次收到错误.
使用top
命令,free -m
我可以看到,我有超过192 MB的免费,所以为什么我仍然得到这个错误?
Mem: 262144k total, 64760k used, 197384k free, 0k buffers
Swap: 0k total, 0k used, 0k free, 0k cached
Run Code Online (Sandbox Code Playgroud)
谢谢
两者之间有什么区别吗?
public class Controller1 extends AbstractController {
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
return new AnotherController().handleRequest(request, response);
}
}
Run Code Online (Sandbox Code Playgroud)
和
@Controller
public class Controller1 {
@RequestMapping ...
public String handleRequest() {
return "forward:/path_to_my_another_controller";
}
}
Run Code Online (Sandbox Code Playgroud) 关于java HashSet的新手问题
Set<User> s = new HashSet<User>();
User u = new User();
u.setName("name1");
s.add(u);
u.setName("name3");
System.out.println(s.contains(u));
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么这段代码输出错误?而且这段代码甚至不调用User的equals方法.但根据HashSet和HashMap的来源,它必须调用它.方法等于用户只需在用户名上调用equals.方法hashCode返回用户名的hashCode
我有手写的安全性,简单的servlet过滤器,它将未经授权的用户重定向到他们的登录页面.登录控制器在成功验证或其主页面后将它们重定向到请求的URL.这种方法工作正常,唯一的缺点是,我必须通过堆栈跟踪将存储在HttpSession中的User对象传递给EJB bean.
现在我重写了一些代码并使用Spring-security作为基于http的身份验证.它与Glassfish JAAS自动集成.
我不再需要通过stacktrace传递User,调用sessionContext.getCallerPrincipal()
就足够了.但是主要对象只返回userName,而不是userId,所以如果我需要userId,我必须执行加法选择.
1)无论如何都有扩展Principal对象,所以它可以存储更多属性?
2)为什么我应该使用JAAS或Spring Security或其他安全框架,为什么不只是手写servlet过滤器?
在我的项目中,我使用Hibernate进行程序化事务划分.每次在我的服务方法中,我都会写一些类似的东西.
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
.. perform operation here
session.getTransaction().commit();
Run Code Online (Sandbox Code Playgroud)
现在,我将使用声明式事务管理来重构我的代码.我现在得到了什么......
<context:component-scan base-package="package.*"/>
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="mySessionFactory"/>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
服务类:
@Service
public class TransactionalService {
@Autowired
private SessionFactory factory;
@Transactional
public User performSimpleOperation() {
return (User)factory.getCurrentSession().load(User.class, 1L);
}
}
Run Code Online (Sandbox Code Playgroud)
而且简单的测试 -
@Test
public void useDeclarativeDem() {
FileSystemXmlApplicationContext ctx = new FileSystemXmlApplicationContext("spring-config.xml");
TransactionalService b = (TransactionalService)ctx.getBean("transactionalService");
User op = b.performSimpleOperation();
op.getEmail();
Run Code Online (Sandbox Code Playgroud)
当我尝试在Transactional方法之外获取用户电子邮件时,我得到了Lazy初始化异常,电子邮件是我的情况是一个简单的字符串.Hibernate甚至不执行sql查询,直到我在我的POJO上调用任何getter.
1)我在这里做错了什么?
2)这种方法有效吗? …
我有一个巨大的Maven多模块项目.具有类似的结构
parentA
- subA1
- subA3
- subA3
- subA2
parentB
- subB1
- subB2
Run Code Online (Sandbox Code Playgroud)
等等
问题是,parentA和subA1不包含其中的任何代码,但我仍然可以在项目资源管理器中看到它们.但我想隐藏它们或像在探险家中那样组织树形层次结构.现在有超过30个项目,其中只有20个包含Java代码.其他人只是包含其他模块参考.我在项目资源管理器中看到了所有30个项目.
第一个想法 - 使用工作集,但我不能在另一个工作集中包含工作集.为eclipse用户处理此问题的常见做法是什么?
我们得到了高集负的java应用程序,它在集群模式下工作.
我需要为我们的客户添加下载和上传文件的功能.对于存储文件,我要去用户gridFs,不确定,这是最好的选择,但mongo可以集群化,mongo可以在diff节点之间复制数据.这正是我需要的.
不同的用户群应该受限于不同的带宽.根据一些业务规则,我应该限制某些用户的下载速度.我看到一些解决方案,此
他们中的大多数工作方式相同.
Mongo只是简单地为我提供了InputStrem,我可以从该流中读取并写入servlet输出流.我不确定这是有效的方法.另外我担心,用户可以在下载过程中创建大量的concurent线程,这可能会影响性能.
这可能是servlet容器的问题吗?
如果它可能是一个问题,怎么可以避免?可能使用nio?
我更喜欢使用纯java解决方案.
任何帮助将受到高度赞赏.
我正在尝试使用"服务帐户"授权访问来调用任何Google API.我在Google API控制台的"服务"标签中下载了".pk2"文件并激活了"URL Shortener API".每当我尝试调用任何API(URL缩短器或Adsense)时.我有以下例外 -
com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request
{
"error" : "invalid_grant"
}
at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:105)
at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:303)
at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:323)
at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:345)
at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:526)
at com.google.api.client.auth.oauth2.Credential.intercept(Credential.java:287)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:836)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:412)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:345)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:463)
Run Code Online (Sandbox Code Playgroud)
以下是代码片段 -
HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
JsonFactory JSON_FACTORY = new JacksonFactory();
File privateKey = new File(ReportAdsense.class.getResource("mykey.p12").toURI());
GoogleCredential credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId("my_valid_account_id@developer.gserviceaccount.com")
.setServiceAccountScopes(UrlshortenerScopes.URLSHORTENER)
.setServiceAccountPrivateKeyFromP12File(privateKey)
.build();
Urlshortener service = new Urlshortener.Builder(new NetHttpTransport(), JSON_FACTORY, null).setHttpRequestInitializer(credential).build();
UrlHistory history = service.url().list().execute();
Run Code Online (Sandbox Code Playgroud) 考虑简单的应用程序,即显示文件列表。我想显示上下文菜单,当用户单击文件或文件夹时,与用户在 Windows 资源管理器中单击鼠标右键(打开、重命名、编辑等)时相同。我不想创建自己的上下文菜单,我想显示 windows os 上下文菜单,可能使用某种 winapi。这应该只适用于 Windows 机器。
是否可以使用 Swing 或 SWT 或任何其他 UI 控件来实现它?