小编Val*_*lva的帖子

Google App Engine - 如何将对象返回给我的servlet?

有没有人知道如何发送一个对象,更具体的一个List,从数据库中的查询得到的结果,到我的servlet,这是另一个Java应用程序,而不是在Google App Engine中.


更新:我在GAE中的servlet工作正常,它序列化我的List<Video>结果:

public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {

    String titulo = req.getParameter("titulo");
    String json = "";

    PersistenceManager pm = PMF.get().getPersistenceManager();
    Query query = pm.newQuery("select from "+Video.class.getName()+ " where titulo.startsWith('"+titulo+"')");
    List<Video> video = (List<Video>) pm.newQuery(query).execute();

    json = new Gson().toJson(video);
    System.out.println("SERIALIZED >> " + json);

    res.setContentType("application/json");
    res.setCharacterEncoding("UTF-8");
    res.getWriter().write(json);
}
Run Code Online (Sandbox Code Playgroud)

我的调用servlet有这个方法:

public void receberMetaDados(String titulo) throws IOException, Exception{
    InputStream input = new URL("http://localhost:8888/serve?titulo="+titulo).openStream();
    Reader reader = new InputStreamReader(input, "UTF-8");
    List<Video> results = new …
Run Code Online (Sandbox Code Playgroud)

java google-app-engine jsp servlets

2
推荐指数
1
解决办法
4404
查看次数

如何处理Java Web应用程序中的异常?

我想以警告用户发生的错误的方式处理我的异常.

我所知道的是:

servlet.java
private void registerUser(...){
    ...
    try{
        DAOUser daoUser = new DAOUser();
        daoUser.insert(user);
    catch(HibernateException he){
        ...
    }

DAOUser.java
public void insert(User user) throws HibernateException {
  ...
}
Run Code Online (Sandbox Code Playgroud)

这是最好的方法吗?如果没有,你会建议什么?

此致,Valter Henrique.

java jsp servlets exception java-ee

2
推荐指数
1
解决办法
2746
查看次数

什么意思';' 在声明里面?

我正在尝试理解以下代码:

/**
   * Simple insertion sort.
   * @param a an array of Comparable items.
   */
  public static void insertionSort( Comparable [ ] a )
  {
      for( int p = 1; p < a.length; p++ )
      {
          Comparable tmp = a[ p ];
          int j = p;
          for( ; j > 0 && tmp.compareTo( a[j-1] ) < 0; j-- )
              a[ j ] = a[ j - 1 ];
          a[ j ] = tmp;
      }
  }
Run Code Online (Sandbox Code Playgroud)

但我不确定是什么意思,for( ; ) 所以我需要你的帮助.对不起,如果它是重复的,但我在这里和谷歌搜索,但到目前为止没有.

java syntax insertion-sort

2
推荐指数
1
解决办法
114
查看次数

Web Filter阻止RichFaces

我创建了一个过滤器,它工作正常,但我的richfaces不再正常工作,这是我的 web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>SuaParte</display-name>
    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>

    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>

    <context-param>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>server</param-value>
    </context-param>

    <context-param>
        <param-name>org.richfaces.SKIN</param-name>
        <param-value>blueSky</param-value>
    </context-param>

    <context-param>
        <param-name>org.richfaces.CONTROL_SKINNING</param-name>
        <param-value>enable</param-value>
    </context-param>

    <filter>
        <display-name>RichFaces Filter</display-name>
        <filter-name>richfaces</filter-name>
        <filter-class>org.ajax4jsf.Filter</filter-class>
    </filter> 

    <filter-mapping> 
       <filter-name>richfaces</filter-name> 
       <servlet-name>Faces Servlet</servlet-name>
       <dispatcher>REQUEST</dispatcher>
       <dispatcher>FORWARD</dispatcher>
       <dispatcher>INCLUDE</dispatcher>
    </filter-mapping>

    <context-param>
        <param-name>com.sun.faces.disableVersionTracking</param-name>
        <param-value>true</param-value>
    </context-param>

    <context-param>
        <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
        <param-value>true</param-value>
    </context-param>


    <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>*.xhtml</url-pattern>
    </servlet-mapping>

    <filter>
        <filter-name>LoginFilter</filter-name>
        <filter-class>filter.LoginFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>LoginFilter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)

我也在这里尝试@BalusC建议,将web.xml更改为:

<filter> …
Run Code Online (Sandbox Code Playgroud)

jsf web.xml richfaces jsf-2 servlet-filters

2
推荐指数
1
解决办法
1676
查看次数

如何在Java的正则表达式中使用OR?

我想接受这两种情况,I_LOVE_TO_CODE.txt或者I_LOVE_TO_CODE_20151125.txt.我知道如何分别为每一个做:

^I_LOVE_TO_CODE.txt$
^I_LOVE_TO_CODE_\d{8}\.txt$
Run Code Online (Sandbox Code Playgroud)

但是如何OR在单个正则表达式中插入条件?

java regex

2
推荐指数
1
解决办法
75
查看次数

在哪里使用逐个功能的约定来放置我的框架类?

我一直在阅读很多关于逐个功能命名约定的内容.所以我决定尝试一个新项目.但是,我不确定它应该如何命名我的大多数类将使用的包,因为我使用的是一个巨大的框架,例如SpringHibernate.

这是我们的Spring contexts类处理方式:

在此输入图像描述

我们的database访问类,管理连接的访问​​类等.

在此输入图像描述

我有一个关于这个的草案:使用这些框架的通用包,例如:

com.company.project.common.spring
com.company.project.common.database
Run Code Online (Sandbox Code Playgroud)

但我担心这仍然有点像package-by-layer.:) feature classes应该如何创建我将访问的包?

java spring hibernate naming-conventions package

2
推荐指数
1
解决办法
475
查看次数

C:使用scanf的方法

我可以scanf(...)用作函数的参数吗?像这样:

printInteger(scanf(....));
Run Code Online (Sandbox Code Playgroud)

我可以使用scanf将我读取的值归因于某个变量吗?像这样:

n = scanf(...);
Run Code Online (Sandbox Code Playgroud)

ps:我在这里解释为什么我这么问.

我知道这个问题可能有点奇怪,但我正在开发一个项目,它正在开发一个编译器,它将一些语言作为输入,然后编译为C.

例如,这是我的语言,我们称之为'stackoverflow';)

proc printInteger(integer k)
    integer i;
        begin
            for i = 1 to k do
                print i;
        end

proc main()
    integer n, i;
    boolean ok;
    begin
        printInteger(getInteger);
        n = getInteger;
        ok = true;
        while i < n do
            begin
                print i;
                i = i + 1;
            end
        if ok then print 1; else print 0;
    end
Run Code Online (Sandbox Code Playgroud)

我不会深入学习语言,但请注意,这getInteger意味着我想做一个scanf(...),我的意思是,当getInteger我想要编译时scanf(...),这就是为什么我想知道一些使用方法scanf(...).

c io input scanf

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

如何在 Vagrant 中使用本地环境变量?

我像这样传递我的本地环境变量:

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure(2) do |de|

  de.vm.box = 'ubuntu/trusty64'
  de.vm.hostname = 'virtual_machine'
  de.vm.network 'public_network', bridge:ENV['NETWORK_INTERFACE'], ip:'192.168.2.170'

  de.vm.provider "virtualbox" do |v|
    v.memory = 4096
    v.cpus = 2
  end

  de.vm.synced_folder '.', '/vagrant', disabled:true
  de.vm.synced_folder '../../synced/shared/', '/shared/'
  de.vm.synced_folder '../../synced/devops/', '/devops/'

  install = ENV['DEVOPS_HOME'] + '/vagrant/lib/install'
  de.vm.provision 'shell', path: install + '/basic'
  de.vm.provision 'shell', path: install + '/java8', args: ['automatic']
  de.vm.provision 'shell', path: install + '/aws_cli', args: [ENV['S3_AWS_ACCESS_KEY_ID'],ENV['S3_AWS_SECRET_ACCESS_KEY']]

  setup = ENV['DEVOPS_HOME'] + '/vagrant/lib/setup'
  de.vm.provision 'shell', path: …
Run Code Online (Sandbox Code Playgroud)

ruby ubuntu vagrant vagrantfile

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

如何在PowerShell上捕获异常?

我几乎同时运行这个PowerShell脚本两次:

#Provision Resource Group and all Resources within ARM Template
New-AzureRmResourceGroupDeployment -ResourceGroupName $ResourceGroupName `
-TemplateFile $TemplateFile `
-Mode Incremental `
-TemplateParameterFile $TemplateParametersFile @ExtraParameters `
-keyVaultAccessPolicies @($keyVaultPoliciesList) `
-Force
Run Code Online (Sandbox Code Playgroud)

这引发了这个异常:

VERBOSE: 16:48:48 - Template is valid.
New-AzureRmResourceGroupDeployment : Unable to edit or replace deployment 'DeploymentTemplate': previous deployment from '5/18/2017 2:48:47 PM' is still active (expiration time is '5/25/2017 2:48:46 PM'). Please see https://aka.ms/arm-deploy for usage details.
At L:\Source\Start-ArmEnvironmentProvisioning.ps1:274 char:1
+ New-AzureRmResourceGroupDeployment -ResourceGroupName $ResourceGroupN ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) …
Run Code Online (Sandbox Code Playgroud)

powershell azure

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

如何在Powershell上重用param?

假设我有以下功能:

function Get-DBStatus
{
  <# .. removed help section for brevity .. #>
  [CmdletBinding()]
  [OutputType([System.Object])]
  param
  (
    [Parameter(Mandatory = $true)]
    [String]$ServerName,
    [Parameter(Mandatory = $true)]
    [String]$ServerUser,
    [Parameter(Mandatory = $true)]
    [String]$ServerPassword,
    [Parameter(Mandatory = $true)]
    [String]$DatabaseName,
  )

  try
  {
    $params = @{ ... } # <<< It's possible to avoid this duplication ?
    $dbStatus = Invoke-SqlConnection @params
  }
  catch
  {
    Write-Error -Message ('An error has occured while ...')

  }
  ...
Run Code Online (Sandbox Code Playgroud)

@params一旦我的参数已经被声明和设置,我想避免声明.使用Powershell可以做到这一点吗?

powershell

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