小编Tha*_*ham的帖子

Java:已检查vs未经检查的异常说明

我已经在StackOverFlow上阅读了有关已检查和未经检查的异常的多个帖子.老实说,我还是不太确定如何正确使用它们.

Joshua Bloch在" Effective Java "中说过

对可恢复条件使用已检查的异常,对编程错误使用运行时异常(第2版中的第58项)

让我们看看我是否正确理解这一点.

以下是我对已检查异常的理解:

try{
    String userInput = //read in user input
    Long id = Long.parseLong(userInput);
}catch(NumberFormatException e){
    id = 0; //recover the situation by setting the id to 0
}
Run Code Online (Sandbox Code Playgroud)

1.以上是否考虑了检查异常?

2. RuntimeException是未经检查的异常吗?

以下是我对未经检查的异常的理解:

try{
    File file = new File("my/file/path");
    FileInputStream fis = new FileInputStream(file);   
}catch(FileNotFoundException e){

//3. What should I do here?
    //Should I "throw new FileNotFoundException("File not found");"?
    //Should I log?
    //Or should I System.exit(0);?
}
Run Code Online (Sandbox Code Playgroud)

4.现在,上述代码也不能成为检查异常吗?我可以尝试恢复这样的情况吗?我可以吗?(注意:我的第3个问题在catch上面)

try{
    String …
Run Code Online (Sandbox Code Playgroud)

java exception runtimeexception checked-exceptions unchecked-exception

678
推荐指数
11
解决办法
31万
查看次数

如何使用JSP/Servlet将文件上传到服务器?

如何使用JSP/Servlet将文件上传到服务器?我试过这个:

<form action="upload" method="post">
    <input type="text" name="description" />
    <input type="file" name="file" />
    <input type="submit" />
</form>
Run Code Online (Sandbox Code Playgroud)

但是,我只获取文件名,而不是文件内容.当我添加 enctype="multipart/form-data"<form>,然后request.getParameter()返回null.

在研究期间,我偶然发现了Apache Common FileUpload.我试过这个:

FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = upload.parseRequest(request); // This line is where it died.
Run Code Online (Sandbox Code Playgroud)

不幸的是,servlet抛出了一个没有明确消息和原因的异常.这是堆栈跟踪:

SEVERE: Servlet.service() for servlet UploadServlet threw exception
javax.servlet.ServletException: Servlet execution threw an exception
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:313)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at …
Run Code Online (Sandbox Code Playgroud)

java jsp servlets file-upload java-ee

671
推荐指数
7
解决办法
54万
查看次数

如何为UILabel添加换行符?

让我们看看我的字符串看起来像这样:

NSString *longStr = @"AAAAA\nBBBBB\nCCCCC";  
Run Code Online (Sandbox Code Playgroud)

我该怎么做才能让UILabel显示这样的信息

AAAAA
BBBBB
CCCCC

我不认为\nUILabel会认可,所以我可以在NSString中放置任何东西,以便UILabel知道它必须在那里创建换行符吗?

iphone newline line-breaks uilabel ios

253
推荐指数
11
解决办法
25万
查看次数

将String.split()与多个分隔符一起使用

我需要拆分的分隔符串基地-..以下是我想要的输出.

AA.BB-CC-DD.zip - >

AA
BB
CC
DD
zip 
Run Code Online (Sandbox Code Playgroud)

但我的以下代码不起作用.

private void getId(String pdfName){
    String[]tokens = pdfName.split("-\\.");
}
Run Code Online (Sandbox Code Playgroud)

java regex

188
推荐指数
7
解决办法
32万
查看次数

List,List <?>,List <T>,List <E>和List <Object>之间的区别

之间有什么区别List,List<?>,List<T>,List<E>,和List<Object>

现在我不要盲目地问这个问题,所以请不要关闭这个帖子.我先介绍一下基本代码:

public static void test(List<?> list){
    System.out.println(list);   // Works
}
Run Code Online (Sandbox Code Playgroud)

我明白:

1 List.:是原始类型,因此不是typesafe.它只会在强制转换时生成运行时错误.当演员表不好时我们想要编译时错误.不建议使用.

2 . List<?>:是一个无界的通配符.但不确定这是为了什么?所以如果我改变List<?>方法

public static void test(List<?> list){
    list.add(new Long(2));     // Error
    list.add("2");             // Error
    System.out.println(list);
}
Run Code Online (Sandbox Code Playgroud)

它仍然很好.如果您能解释一下这种用法,我将不胜感激.

编辑:如果我这样做:

public static void test(List<T> list){   // T cannot be resolved
    System.out.println(list);
}
Run Code Online (Sandbox Code Playgroud)

但如果我改成List<?>这个:

public <T> T[] toArray(T[] a){
    return a;   
}
Run Code Online (Sandbox Code Playgroud)

3 . <T>:

public static …
Run Code Online (Sandbox Code Playgroud)

java generics

188
推荐指数
8
解决办法
15万
查看次数

请解释一下:insertable = false,updatable = false

如果某个字段有注释insertable=false, updatable=false,这是否意味着您不能插入值也不能更改现有值?你为什么想这么做?

@Entity
public class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @OneToMany(mappedBy="person", cascade=CascadeType.ALL)
    private List<Address> addresses;
}

@Entity
public class Address {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @ManyToOne
    @JoinColumn(name="ADDRESS_FK")
    @Column(insertable=false, updatable=false)
    private Person person;
}
Run Code Online (Sandbox Code Playgroud)

java jpa eclipselink java-ee

132
推荐指数
5
解决办法
12万
查看次数

如何发送HTML电子邮件?

我已使用JMS在我的Web应用程序中成功发送了电子邮件,但结果仅以纯文本显示.我希望内容能够显示html.我该怎么做?这大致是我所拥有的:

Message msg = new MimeMessage(mailSession);
try{
    msg.setSubject("Test Notification");
    msg.setRecipient(Message.RecipientType.TO, new InternetAddress(sentTo, false));
    String message = "<div style=\"color:red;\">BRIDGEYE</div>";
    msg.setContent(message, "text/html; charset=utf-8");
    msg.setSentDate(new Date());
    Transport.send(msg);
}catch(MessagingException me){
    logger.log(Level.SEVERE, "sendEmailNotification: {0}", me.getMessage());
}
Run Code Online (Sandbox Code Playgroud)

java email jakarta-mail

115
推荐指数
5
解决办法
19万
查看次数

JPA:如何将String保存到数据库字段中,键入MYSQL Text

要求是用户可以写一篇文章,因此我选择mysql数据库中Textcontent字段类型.我怎样才能转换Java StringMySQL Text

干得好 Jim Tough

@Entity
public class Article implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private Long userId;

    private String title;

    private String content;

    private Integer vote;

    //Constructors, setters, getters, equals and hashcode
}
Run Code Online (Sandbox Code Playgroud)

在我的MYSQL数据库中,content是类型Text.我希望会有这样的东西java.sql.Text,因为它java.sql.Blob是一个真实的类型,但遗憾的是,这不存在

java mysql orm jpa

73
推荐指数
3
解决办法
11万
查看次数

有时我看到JSF URL是*.jsf,有时是*.xhtml,有时是/ faces/*.为什么?

一直试着学习JSF,有时候我看到的URL *.jsf有时是*.xhtml或者/faces/*.请问有人填写我的知识吗?当我使用Facelet创建JSF时,文件扩展名是.xhtml,所以.jsfURL扩展来自哪里?

jsf web.xml facelets url-pattern

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

当用户登录Web应用程序时,如何实现"保持登录状态"

在大多数网站上,当用户即将提供登录系统的用户名和密码时,会出现一个"保持登录状态"的复选框.如果选中该复选框,它将使您在同一Web浏览器的所有会话中登录.如何在Java EE中实现相同的功能?

我正在使用基于FORM的容器管理身份验证和JSF登录页面.

<security-constraint>
    <display-name>Student</display-name>
    <web-resource-collection>
        <web-resource-name>CentralFeed</web-resource-name>
        <description/>
        <url-pattern>/CentralFeed.jsf</url-pattern>
    </web-resource-collection>        
    <auth-constraint>
        <description/>
        <role-name>STUDENT</role-name>
        <role-name>ADMINISTRATOR</role-name>
    </auth-constraint>
</security-constraint>
 <login-config>
    <auth-method>FORM</auth-method>
    <realm-name>jdbc-realm-scholar</realm-name>
    <form-login-config>
        <form-login-page>/index.jsf</form-login-page>
        <form-error-page>/LoginError.jsf</form-error-page>
    </form-login-config>
</login-config>
<security-role>
    <description>Admin who has ultimate power over everything</description>
    <role-name>ADMINISTRATOR</role-name>
</security-role>    
<security-role>
    <description>Participants of the social networking Bridgeye.com</description>
    <role-name>STUDENT</role-name>
</security-role>
Run Code Online (Sandbox Code Playgroud)

forms-authentication java-ee stay-logged-in

53
推荐指数
2
解决办法
4万
查看次数