标签: servlets

这段代码有什么缺陷吗?

我编写了一个servlet,但servlet还没有处于生产阶段.

我在servlet的Filter中添加了一个计数器,这样当并发请求数达到限制时,就不能再接受任何人了.我担心一些边缘情况,例如:假设系统已经达到49个并发请求(50个是最大值),并且在synchronized块中它使布尔变量"ok"为True,然后在下一个实例中,多个线程看到servlet是可用的,并急于进入并打破限制.

如果有任何缺陷,请帮助检查此代码:

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        // TODO Auto-generated method stub
        // place your code here

        // pass the request along the filter chain
        conditionalInfoLog(logEnabled, "Incoming request...");
        conditionalInfoLog(logEnabled, "Number of concurrent request(s): " + count);
        boolean ok;
        synchronized (lock) {
            ok = count < limit;
            if(ok){
                count++;
            }
        }
        if (ok) {
            try{
                // let the request through and process as usual
                conditionalInfoLog(logEnabled, "Request accepted and processing, number of concurrent request(s): …
Run Code Online (Sandbox Code Playgroud)

java concurrency servlets

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

如何从数据库中检索图像并通过Servlet在JSP中显示?

我的ImageDAO看起来像这样:

public InputStream getPhotos(Long userid) throws 
  IllegalArgumentException, SQLException, ClassNotFoundException {

  Connection connection = null;
  PreparedStatement preparedStatement = null;
  ResultSet resultset = null;
  Database database = new Database();
  InputStream binaryStream = null;

  try {

    connection = database.openConnection();
    preparedStatement = connection.prepareStatement(SQL_GET_PHOTO);                  
    preparedStatement.setLong(1, userid);
    preparedStatement.executeUpdate();

    while(resultset.next()) {
      binaryStream = resultset.getBinaryStream(4);
    }

  } catch (SQLException e) {
      throw new SQLException(e);
  } finally {
      close(connection, preparedStatement, resultset);
  }
  return binaryStream;
}
Run Code Online (Sandbox Code Playgroud)

我的ImageServlet看起来像这样:

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

  // Getting user …
Run Code Online (Sandbox Code Playgroud)

java jsp servlets image

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

为什么我得到"此方法不支持HTTP方法GET"错误?

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class LoginServlet extends HttpServlet
{
private void sendLoginPage (HttpServletResponse res, HttpServletRequest req, boolean error)
throws ServletException,IOException
{
    res.setContentType("text/html");
    PrintWriter o = res.getWriter();        
    o.println("<html><head><title>Sample Login Page</title></head><body>Welcome to Login Page : ");
    if (error)
    o.println("<fieldset><legend>Login Form : </legend>");
    o.println("Login Failed, Please Try Again");
    o.println("<form method="+"post"+">");
    o.println("<br/><input type="+"text"+"value="+"username"+"/>");
    o.println("<br/><input type="+"password"+"value="+""+"/>");
    o.println("<br/><input type="+"button"+"value="+"Submit"+"/>");
    o.println("</form></fieldset></body></html>");
}

public void doGet (HttpServletResponse res, HttpServletRequest req)
throws ServletException,IOException
{
    sendLoginPage ( res, null, false ) ;
}

public void …
Run Code Online (Sandbox Code Playgroud)

java servlets

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

当路径中的类时,Servlet抛出java.lang.ClassNotFoundException

我正在编写一个简单的servlet,并尝试在代码中创建我的一个类的实例DataStore.

此类是公共的,位于DataStore.java与Servlet代码相同的包中调用的文件中.

当我尝试在代码中创建一个新实例时:

DataStore dStore = new DataStore();
Run Code Online (Sandbox Code Playgroud)

我得到以下异常:

java.lang.ClassNotFoundException: backend.DataStore
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
    at backend.AjaxServlet.createGame(AjaxServlet.java:196)
Run Code Online (Sandbox Code Playgroud)

我尝试在一个不同的文件中创建这个类的实例,它工作得很好.知道这可能是什么原因?

java servlets java-ee

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

此URL不支持HTTP方法GET

我正在使用jsp页面的超链接访问注销servlet时遇到代码问题.

Jsp页面链接:

HREF = "/注销"

注销Servlet:

public class logOut extends HttpServlet{

public void doGET(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {

    resp.setContentType("text/html"); 
    System.out.println("log out servlet");
    HttpSession session = req.getSession(false);
    if (session != null) {
        session.invalidate();
    }
    resp.sendRedirect("/signin.jsp");
}
}
Run Code Online (Sandbox Code Playgroud)

但我有以下错误:

HTTP ERROR 405

Problem accessing /logout. Reason:

HTTP method GET is not supported by this URL
Run Code Online (Sandbox Code Playgroud)

请帮我.....

java servlets

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

从java上的查询字符串中获取带有"#"字符的参数

我试图获取一个具有该字符的请求字符串,#并且我的参数只有#.但问题是我需要有这个角色,不能删除它.

任何的想法?

java servlets query-string http-request

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

在应用程序启动时启动所有servlet

我有一个Web应用程序,它有两个servlet,通过上下文属性交换信息.

每个servlet都依赖于另一个servlet在上下文属性中设置的信息.例如,如果我第一次访问servlet S1,我需要访问servlet S2提供的信息,通过上下文属性,我URLConnection向S2 发出请求(通过).

我需要servlet S1和S2在应用程序启动时启动(初始化),而不是在向它们发出请求时.

我可以进行任何配置,以便我的应用程序中的所有servlet在启动时初始化吗?

java servlets initialization

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

想要创建一个servlet,它将基于guid文件名将发布的数据保存到文件中

所以我将我的java应用程序推送到服务器,非常兴奋.

现在我想测试一下,如何将发布的数据保存到我的servlet文件中,文件名应该是一个唯一的guid.

到目前为止我有这个:

public class TestServlet extends javax.servlet.http.HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
            throws javax.servlet.ServletException, IOException {

    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
            throws javax.servlet.ServletException, IOException {

        PrintWriter printWriter = response.getWriter();

        printWriter.print("hello, world from testservlet!");


    }
}
Run Code Online (Sandbox Code Playgroud)

因此,假设http发布数据(比如大约50K)将被发布到字段"有效负载",我如何获取发布的文本,并将其保存到文件中,文件名为GUID.

java有一个构造来清理一个打开的文件,比如在c#中:

using(var file = new ....)
{
  // write to file
}
Run Code Online (Sandbox Code Playgroud)

这会关闭连接并清理内存等.

另外,我是否需要为tomcat设置特殊权限才能保存此文件?

我现在默认设置(只是在VPS上玩)使用ubuntu 11,安装tomcat6.

谢谢.

java file-io tomcat servlets

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

协助编写servlet的文件编写器方法

我有一个简单的servlet,如果它有一个查询参数'hello',我写一个文件,因为这是一个测试,我想在网页上也显示错误.

IntelliJ抱怨我没有捕获IOException,不知道出了什么问题:

private static void WriteToFile(String filePath, String fileName, String fileData) {

    FileWriter writer = null;

    try {
        writer = new FileWriter(fileName);
        writer.write(fileData);
    } catch(IOException ex) {


    } finally {
        if(writer != null) {
            writer.close();
        }

    }


}
Run Code Online (Sandbox Code Playgroud)

另外,在我的例外情况中,我注意到大多数人在网上写道:

如何将错误输出到网页?

java file-io servlets

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

单例和同步Servlet

我想创建一个处理串行设备输入的Servlet,因此我想确保一次只有一个Servlet实例存在于容器中(无论容器是否只生成一个实例,我必须确保())以及对串口的访问是同步还是序列化.

有什么建议?

java singleton servlets

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