标签: servlets

Servlet:获取查询字符串但只返回空

我正在为我的web项目使用mediator设计模式.有一个前端控制器来处理所有请求,并根据每个请求找到合适的servlet.在web.xml中,我试试这个:

<servlet-mapping>
         <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/*</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)

然后像 localhost:8080/project_name/a/b/c/d.jsp这样的请求我想得到字符串a/b/c/d.jsp但是当我使用时: request.getServletPath()我得到空字符串.

谢谢 :)

java servlets

0
推荐指数
1
解决办法
358
查看次数

JSP错误"非法启动类型"

我正在尝试运行一个运行HTML-Form的JSP文件,但是我遇到了以下问题:当我尝试在testserver上运行此文件(使用LiveLink运行)时,会出现一个错误,说:


500 Servlet Exception

/WebApps_e/WebApps/FORMS/Formular_Softwareantrag/PDFTest/PDFTest.jsp:34:
illegal start of type

    try
    ^
f:\wcm\website\WEB-INF\work\_jsp\_webapps_0e\_webapps\_forms\_formular_0softwareantrag\_pdftest\_pdftest__jsp.java:319:

<identifier> expected
  private java.util.ArrayList _caucho_depends = new java.util.ArrayList();
                                                                         ^

2 errors
Run Code Online (Sandbox Code Playgroud)

这就是我的JSP-File的样子

<%@ page import="
java.util.*,
java.util.HashMap,
java.net.URL,
java.io.*,
javax.mail.*,
javax.mail.internet.*,
javax.activation.*,
de.gauss.vip.portalmanager.VipObjectBean,
de.gauss.vip.repository.RepositoryEntry,
de.gauss.lang.StringValue,
de.gauss.vip.api.admin.Server,
com.lowagie.text.*,
com.lowagie.text.pdf.*,
com.caucho.vfs.*
" %>
<%!
HashMap pdfOutputs = new HashMap();
Document document = null;
PdfReader reader = null;
PdfStamper stamper = null;
AcroFields acro_fields = null;
ByteArrayOutputStream bostream = null;

try
{
    vobFORMS.setRepositoryName("{VIPDEPLOYMENT_NAME}");
    vobFORMS.addDefaultAttribute("pathname");

    /** Check for standart attributes */
    String …
Run Code Online (Sandbox Code Playgroud)

java jsp servlets

0
推荐指数
1
解决办法
744
查看次数

为什么getRemoteHost()在部署应用程序时会返回IP地址?

在我的开发环境中,getRemoteHost()成功返回客户端的PC /计算机名称。但是,一旦我将应用程序部署到生产环境中,getRemoteHost()就会突然返回IP地址。

为了使其始终返回PC /计算机名称,需要采取什么措施?它是在WAS 7.0上运行的Java Web应用程序。

java websphere servlets ibm-was

0
推荐指数
1
解决办法
1080
查看次数

将嵌套数组从PHP传递到Java servlet

我有一个数组$标签,我打印使用:

foreach($tags as $t) {
                echo $t['token'] . "/" . $t['tag'] .  " ";
        }
Run Code Online (Sandbox Code Playgroud)

我怎样才能将这个$标签传递给Java servlet,所以在Java程序中我可以像在PHP脚本中一样使用foreach$t['token']$t['tag']

我假设必须使用POST方法完成,是否也可以使用GET?

更新
了json_array:

$js_array = json_encode($tags);
echo "var javascript_array = ". $js_array . ";\n";
Run Code Online (Sandbox Code Playgroud)

哪个回报:

var javascript_array = [{"token":"test","tag":"NN"},{"token":"1","tag":"NN"}];
Run Code Online (Sandbox Code Playgroud)

我试图通过使用以下方法将其传递给servlet:

<script src="http://code.jquery.com/jquery-1.10.1.min.js">

$( document ).ready(function() {
    alert('ok');

                $.ajax({
                    url : "http://localhost:8080/first/SPARQL",
                    type: "POST",
                    data: $js_array,                  
                    dataType: "json", 
                    async: false,
                    success: function (){
                        alert( "succes");},
                        error: function(){
                            alert("false");
                        }
                });

});
</script>
Run Code Online (Sandbox Code Playgroud)

但是,它既不返回"成功"或"假".它显示"确定"警报.PS我正在通过Eclipse和Tomcat 8运行java servlet.php文件在我的Wamp localhost上.我可以在浏览器中访问该URL.
此外,似乎我不能在我用PHP制作的javascript中使用$ js_array,它说它没有设置. …

javascript php java jquery servlets

0
推荐指数
1
解决办法
345
查看次数

CQ5 SlingServlet和resourceTypes不适用于特定资源路径

如果我按如下方式定义Sling Servlet:

@SlingServlet(
    label="TestResourceTypeServlet",
    name = "com.company.project.servlets.TestResourceType",
    extensions = {"bob"},
    resourceTypes= {"cq:Page"},
    methods= {"GET"},
    metatype=true)
@Properties({
@Property(name = "service.description", value = "A test servlet"),
@Property(name = "service.vendor",  value = "Company")
})
Run Code Online (Sandbox Code Playgroud)

servlet向每个扩展名为".bob"的页面提取任何get请求,这很好,但我真正想要的是处理对特定页面类型的请求,

所以

我修改resourceTypes来读取

resourceTypes= {"site-administration/components/page/page-distribution"},
Run Code Online (Sandbox Code Playgroud)

提供的值是我尝试使用.bob扩展名访问的页面的特定sling:resourceType(复制并粘贴出CRXDE Lite),但我得到了404!

我读过的所有文档都说上面应该有用,但事实并非如此.

出于绝望,我甚至尝试过"site-administration/components/page"哪种是我想要的页面的超级类型.

我正在使用这个servlet作为OSGi包的一部分运行一个干净的5.6.1实例.

我是否遗漏了一些明显的东西,或者如果没有,是否有人知道任何可以解决此问题的热修复

任何帮助都会受到赞赏,因为我开始有点疯狂.

编辑

好的,所以我进一步了解:如果我访问该页面:

[path-to-page]/page.bob.html
Run Code Online (Sandbox Code Playgroud)

servlet会触发.但是这个URL bob不是选择器吗?如果是这样,为什么资源类型是什么cq:Page配置使用bob作为扩展?

非常困惑:-S

我显然在这里遗漏了一些非常简单的东西.

servlets sling aem

0
推荐指数
1
解决办法
1263
查看次数

如何从ServletContext获取HttpServletRequest?

是否可以从ServletContext获取HttpServletRequest?

java servlets

0
推荐指数
1
解决办法
1万
查看次数

从REST Webservice调用Servlet之前的过滤器

我编写了一个REST Web服务方法setToken(@Context HttpServletRequest request, @Context HttpServletResponse response, @PathParam("token") String token),HeaderFilter并且SampleServlet.下面是Web服务类`@Path("/ service")公共类Service {

/*@Context 
private ServletContext servletContext; 

@Context
private HttpServletRequest request;

@Context
private HttpServletResponse response;*/


@Path("/val/{token}")   
@GET
@Produces("application/xml")
public String setToken(@Context HttpServletRequest request, @Context HttpServletResponse response, @PathParam("token") String token) throws ServletException, IOException {
    String value=token;
    if(request==null){
        System.out.println("Request null");
    }
    System.out.println("Token: " + value);
    if(request!=null){
        request.setAttribute("param", value);
        Wrapper requestWrapper = new Wrapper(request);
        requestWrapper.addHeader("Authorization", token);
        request.getRequestDispatcher("/secure").include(requestWrapper, response);
    }
    return "<token>"+ "<value>"+value+" token value"+"</value>" + "</token>";
}
Run Code Online (Sandbox Code Playgroud)

}` …

java rest web-services servlets servlet-filters

0
推荐指数
1
解决办法
2347
查看次数

javax.naming.NamingException:无法创建资源实例

关于这个问题似乎有几个话题,但是找不到答案。我对JSF不太熟悉,并且想使用以下方法创建一个postgresql连接池

  • JSF 2.2.9
  • Tomcat 8.0.27

但是tomcat总是给我一个HTTP Status 500-实例化servlet类错误的错误(帖子末尾的堆栈跟踪)您能帮我吗?

我的文件:

web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
      <display-name>Postgresql</display-name>
      <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>/faces/*</url-pattern>
      </servlet-mapping>
      <context-param>
        <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>client</param-value>
      </context-param>
      <context-param>
        <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
        <param-value>resources.application</param-value>
      </context-param>
      <listener>
        <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
      </listener>
      <resource-ref>
        <description>Postgres Test</description>
        <res-ref-name>jdbc/einfuehrungsaufgabe</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
      </resource-ref>
    </web-app>
Run Code Online (Sandbox Code Playgroud)

context.xml

<context>

    <Resource name="jdbc/einfuehrungsaufgabe" auth="Container"
        type="javax.sql.Datasource" maxActive="20" maxIdle="5" maxWait="10000"
        username="foo" password="foo" driverClassName="org.postgresql.Driver"
        url="jdbc:postgresql://foo.de/foo">
    </Resource>

</context> …
Run Code Online (Sandbox Code Playgroud)

tomcat servlets jndi exception

0
推荐指数
1
解决办法
1万
查看次数

Servlet验证到Javascript

我怎样才能将值从servlet传递给javascript?

javascript sql jsp servlets

0
推荐指数
1
解决办法
41
查看次数

使用会话和cookie进行Servlet身份验证

我需要实现简单的servlet用户身份验证(在Java动态Web项目中),但有些事情让我感到困惑.

首先,servlet由于某种原因创建了cookie,JSESSIONID尽管我从未要求它.而且,我不能改变它的价值,如果我这样做request.addCookie(new Cookie("JSESSIONID", session.getId())),它会产生这样的东西:

Cookie: JSESSIONID=6B5B441038414B4381EDB7470018F90E; JSESSIONID=7890D45DF445635C49BDEB3CADA8AD99; .......
Run Code Online (Sandbox Code Playgroud)

所以,它复制了cookie.

其次,我不确定在哪里比较cookie和会话的id,以及在何处以及如何正确创建会话(即request.getSession(true? / false? / nothing?);)

我已经阅读了一些文档但仍需要帮助.


我有一个servlet HomeServlet,authentication如果用户未经过身份验证,则会将用户重定向到页面.

这是我如何做到的(HomeServlet.java):

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        if(request.getSession().getAttribute("user") != null) {
            request.getRequestDispatcher("/WEB-INF/index.jsp").forward(request, response);
        } else {
            response.sendRedirect("authentication");
        }
    }
Run Code Online (Sandbox Code Playgroud)

而且我也有AuthServletjsp页面提供身份验证表单并验证用户.

AuthServlet.java:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String action = request.getParameter("ACTION");

    if ("login".equals(action)) {
        String[] result = doSomeValidations();

        if (result.size() > …
Run Code Online (Sandbox Code Playgroud)

java cookies session servlets

0
推荐指数
1
解决办法
3985
查看次数