在我的应用程序中,我有一个 WebFilter。这个 Webfilter 应该检查一个 coockie。但是使用 FacesContext.getCurrentInstance() 会导致 Nullpointer 异常。我该如何解决这个问题?
网络过滤器:
@Inject
private CookieManager cm;
[...]
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if(cm.isDoCheck()){
cm.doCheck();
}
chain.doFilter(request, response);
}
[...]
Run Code Online (Sandbox Code Playgroud)
执行 FacesContext.getCurrentInstance() 的 CookieManager:
[...]
private void doCheck(){
FacesContext context = FacesContext.getCurrentInstance();
Map<String, Object> cookies = context.getExternalContext().getRequestCookieMap();
Cookie cookie = (Cookie) cookies.get("frontend");
if(cookie != null){
setSessionHash(cookie.getValue());
}
}
[...]
Run Code Online (Sandbox Code Playgroud)
context.getExternalContext().getRequestCookieMap(); 给出
StandardWrapperValve[Faces Servlet]: Servlet.service() for servlet Faces Servlet threw exception
java.lang.NullPointerException
Run Code Online (Sandbox Code Playgroud) 我今天在 JSF 上阅读了一篇关于 Lifecycle的文章。
我很难理解这些要点:
1 - 阶段 3:过程验证 - 这是组件可以验证其新值的阶段。如果新值有效并且不同于先前值,则将创建值更改事件并将其放入队列中。所以在我们的例子中,如果用户在提交表单之前更改了名称,则与 Name 文本框对应的 UIInput 组件对象将创建一个 ValueChangeEvent 对象,并在此阶段结束时将其排队等待处理。这就是 valueChangeInput 方法中的支持 bean 被调用。
JSF 如何知道旧值和新值之间的区别?视图对象的实例是 2? 前一个(请求之前的那个)和新的?(其中包含最后一个进程 Apply request Values 添加的 FacesContext 上的值)
2 - 阶段 5:调用应用程序 - 一旦请求的所有值都成功设置到支持 bean,将处理在应用请求值阶段排队的操作事件。在我们的例子中提交按钮操作方法。
因此它直接将 FacesContext 的实例发送到将 UI 元素(及其值)转换为 Html 的最后阶段(渲染响应)。那么,什么时候调用(bean 的)getter 方法?
干杯
mysql代码应该放在哪里MVC?显然不是观点.
通过引入Servlet 3.0,我们可以使用注释将servlet映射到URL模式,并在web.xml中省略映射.
我想知道是否有一些intstructions或特殊标签允许将jsp映射到页面代码中的URL而不在web.xml中声明servlet
我想将我的 servlet 映射到/*,但它因无限循环而失败。
<servlet>
<servlet-name>helloServlet</servlet-name>
<servlet-class>my.HelloServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
Java代码是:
public class HelloServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response){
request.getRequestDispatcher("/WEB-INF/jsps/hello.jsp").forward(request, response);
}
}
Run Code Online (Sandbox Code Playgroud)
如果我映射到/hello,一切正常。
当HelloServlet映射到 时/*,它也会被调用RequestDispatcher#forward()并导致无限循环。
这是怎么引起的,我该如何解决?
配置servlet web.xml通常遵循以下模式:
http://www.mydomain.com/servletName/some/path/here
Run Code Online (Sandbox Code Playgroud)
如果我希望所有请求都转到同一个servlet,我会使用什么配置?
http://www.mydomain.com/some/path/here
Run Code Online (Sandbox Code Playgroud) 我正在尝试学习如何使用Java EE 6构建Web应用程序,但我很难理解在典型MVC2设计的组件之间传递信息的最佳方法.
我理解它的方式,使用Java EE的MVC2模式将是:数据是模型,控制器是servlet,视图是JSP.这只是一个例子.
所以我写了以下三个部分,我知道如何在我正在使用的服务器(Tomcat 7)中安装它们,入口点将是下面的html文件.我正在努力解决servlet如何转发它对JSP的响应,以及如何将JSP发送回客户端浏览器.
HTML文件(demo.html):
<html>
<head>
<title>MVC2 Demo</title>
</head>
<body>
<form method='post' action='/mvc2-demo/DemoServlet'>
<h1> MVC2 Demo </h1>
Name: <input type='text' name='input_name' size='50'>
<br><br>
Age: <input type='text' name='input_age' size='10'>
<br><br>
<input type='submit' value='SUBMIT INFO'>
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
servlet(DemoServlet.java):
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DemoServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) {
response.setContentType("text/html");
try {
PrintWriter pw = response.getWriter();
String name = request.getParameter("input_name");
String age = request.getParameter("input_age");
pw.println("Information received by …Run Code Online (Sandbox Code Playgroud) 我的WAR结构如下:
my-web-app.war/
views/
index.html
blah.html
META-INF/
MANIFEST.MF
WEB-INF/
web.xml
lib/
<!-- Dependencies -->
classes/
org.me.mywebapp.controllers/
MyController.class
<!-- Other packages/classes as well -->
Run Code Online (Sandbox Code Playgroud)
我想配置,web.xml以便在本地部署WAR时,index.html可以通过访问来访问页面http://localhost/my-web-app/index.html.
这是我到目前为止所拥有的:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<!-- The display name of this web application -->
<display-name>My Web App</display-name>
<listener>
<listener-class>
org.me.mywebapp.context.ContextImpl
</listener-class>
</listener>
</web-app>
Run Code Online (Sandbox Code Playgroud)
如何配置此URL以查看映射?提前致谢!
我想要一个可以在没有参数的情况下使用的简单servlet.就像是 :
http://servername:8080/do/view/username/address
Run Code Online (Sandbox Code Playgroud)
并像参数一样使用它:
http://servername:8080/do?action=view&login=username&page=address
Run Code Online (Sandbox Code Playgroud)
两个网址都具有相同的行为.我更喜欢不使用任何框架,只使用servlet和过滤器.
如何从servlet获取url名称?什么是最好的解决方案?
根据@BalusC的回复,我创建了以下servlet,它可以完成我想要的任务:
@WebServlet("/do/*")
public class ActionTestCasesServlet extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
String pathInfo = request.getPathInfo();
String[] parts = pathInfo.substring(1).split("/");
RequestDispatcher destination = getServletContext()
.getRequestDispatcher("/" + parts[0] + ".jsp");
if (parts.length > 1) {
request.setAttribute("username", parts[1]);
}
if (parts.length > 2) {
request.setAttribute("page", parts[2]);
}
destination.forward(request, response);
}
}
Run Code Online (Sandbox Code Playgroud)
此代码调用"view.jsp"传递属性"username"和"page".
在花了一些时间用于servlet和JSP之后,我正在尝试学习一些关于JSF的东西.我已经学习了基础知识,做了几个简单的例子,有一个基本的"工作流程"概念,但我仍然无法理解javax.faces.webapp.FacesServlet的用途.
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
我知道"Faces Servlet"它只是一个"内部"名称,仅用于XML并且它与一个类绑定,在这种情况下:javax.faces.webapp.FacesServlet.但是这个班级到底在哪里?!我正在使用Eclipse,创建了一个新的动态项目,GlassFish 4.0作为服务器,JSF 2.0作为配置(选择了没有库),我也没有导入任何jar.它怎么样?当我尝试用JBoss运行相同的东西时,我必须导入一个javax.faces-2.2.2.jar文件.
好吧,该库可能已经包含在GlassFish中,因为它可以工作但是......如果我尝试在另一台服务器上部署我的应用程序,我会遇到任何问题吗?像JBoss或Websphere.
简而言之:使用JSF技术时的先决条件是什么:)
谢谢.