我已经在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
如何使用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) 让我们看看我的字符串看起来像这样:
NSString *longStr = @"AAAAA\nBBBBB\nCCCCC";
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能让UILabel显示这样的信息
AAAAA
BBBBB
CCCCC
我不认为\n
UILabel会认可,所以我可以在NSString中放置任何东西,以便UILabel知道它必须在那里创建换行符吗?
我需要拆分的分隔符串基地-
和.
.以下是我想要的输出.
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) 之间有什么区别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) 如果某个字段有注释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) 我已使用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) 要求是用户可以写一篇文章,因此我选择mysql数据库中Text
的content
字段类型.我怎样才能转换Java String
成MySQL 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
是一个真实的类型,但遗憾的是,这不存在
一直试着学习JSF,有时候我看到的URL *.jsf
有时是*.xhtml
或者/faces/*
.请问有人填写我的知识吗?当我使用Facelet创建JSF时,文件扩展名是.xhtml
,所以.jsf
URL扩展来自哪里?
在大多数网站上,当用户即将提供登录系统的用户名和密码时,会出现一个"保持登录状态"的复选框.如果选中该复选框,它将使您在同一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) java ×7
java-ee ×3
jpa ×2
eclipselink ×1
email ×1
exception ×1
facelets ×1
file-upload ×1
generics ×1
ios ×1
iphone ×1
jakarta-mail ×1
jsf ×1
jsp ×1
line-breaks ×1
mysql ×1
newline ×1
orm ×1
regex ×1
servlets ×1
uilabel ×1
url-pattern ×1
web.xml ×1