Sun River
Topics about Java SE, Servlet/JSP, JDBC, MultiThread, UML, Design Pattern, CSS, JavaScript, Maven, JBoss, Tomcat, ... |
Question
How do you delete a Cookie within a JSP? (JSP)Answer
Cookie mycook = new Cookie("name","value");
response.addCookie(mycook);
Cookie killmycook = new Cookie("mycook","value");
killmycook.setMaxAge(0);
killmycook.setPath("/");
killmycook.addCookie(killmycook);
Question
How many types of protocol implementations does RMI have? (RMI)Answer
RMI has at least three protocol implementations: JavaRemote Method Protocol(JRMP), Internet Inter ORB Protocol(IIOP),
and Jini Extensible Remote Invocation(JERI). These are alternatives,
not part of the same thing, All three are indeed layer 6 protocols for
those who are still speaking OSI reference model.
Question
What are the different identifier states of a Thread?(Core Java)
Answer
The different identifiers of a Thread are:R - Running or runnable thread
S - Suspended thread
CW - Thread waiting on a condition variable
MW - Thread waiting on a monitor lock
MS - Thread suspended waiting on a monitor lock
Question
What is the fastest type of JDBC driver? (JDBC)Answer
JDBC driver performance will depend on a number ofissues:
(a) the quality of the driver code,
(b) the size of the driver code,
(c) the database server and its load,
(d) network topology,
(e) the number of times your request is translated to a different API.
In general, all things being equal, you can assume that the more your
request and response change hands, the slower it will be. This
means that Type 1 and Type 3 drivers will be slower than Type 2
drivers (the database calls are make at least three translations versus
two), and Type 4 drivers are the fastest (only one translation).
Question
Request parameter How to find whether a parameterexists in the request object?
(Servlets)Answer
1.boolean hasFoo = !(request.getParameter("foo") ==null || request.getParameter("foo").equals(""));
2. boolean hasParameter =
request.getParameterMap().contains(theParameter);
(which works in Servlet 2.3+)
Question
How can I send user authentication information whilemakingURLConnection?
(Servlets)Answer
You’ll want to useHttpURLConnection.setRequestProperty and set all the appropriate
headers to HTTP authorization.
Question
How do I convert a numeric IP address like 192.18.97.39into a hostname like java.sun.com?
(Networking)Answer
Question
How many methods do u implement if implement theSerializable Interface?
(Core Java)Answer
The Serializable interface is just a "marker" interface,with no methods of its own to implement. Other ’marker’ interfaces
are
java.rmi.Remote
java.util.EventListener
String hostname =InetAddress.getByName("192.18.97.39").getHostName(); 1.Question
What is the query used to display all tables names inSQL Server (Query analyzer)?
(JDBC)Answer
select * from information_schema.tablesQuestion
What is Externalizable? (Core Java)Answer
Externalizable is an Interface that extends SerializableInterface. And sends data into Streams in Compressed Format. It has
two methods, writeExternal(ObjectOuput out) and
readExternal(ObjectInput in).
Question
What modifiers are allowed for methods in an Interface?Answer
Only public and abstract modifiers are allowed formethods in interfaces.
Question
How many types of JDBC Drivers are present and whatare they?
(JDBC)Answer
There are 4 types of JDBC DriversType 1: JDBC-ODBC Bridge Driver
Type 2: Native API Partly Java Driver
Type 3: Network protocol Driver
Type 4: JDBC Net pure Java Driver
Question
What is the difference between ServletContext andPageContext?
(JSP) Answer ServletContext: Gives the information about the containerPageContext: Gives the information about the Request.
Question
How to pass information from JSP to included JSP? Answer Using <%jsp:param> tag.tomcat6配置雙向認證
1、生成服務器端證書
keytool -genkey -keyalg RSA -dname "cn=localhost,ou=sango,o=none,l=china,st=beijing,c=cn" -alias server -keypass password -keystore server.jks -storepass password -validity 3650
2、生成客戶端證書
keytool -genkey -keyalg RSA -dname "cn=sango,ou=sango,o=none,l=china,st=beijing,c=cn" -alias custom -storetype PKCS12 -keypass password -keystore custom.p12 -storepass password -validity 3650
客戶端的CN可以是任意值。
3、由于是雙向SSL認證,服務器必須要信任客戶端證書,因此,必須把客戶端證書添加為服務器的信任認證。由于不能直接將PKCS12格式的證書庫導入,我們必須先把客戶端證書導出為一個單獨的CER文件,使用如下命令,先把客戶端證書導出為一個單獨的cer文件:
keytool -export -alias custom -file custom.cer -keystore custom.p12 -storepass password -storetype PKCS12 -rfc
然后,添加客戶端證書到服務器中(將已簽名數字證書導入密鑰庫)
keytool -import -v -alias custom -file custom.cer -keystore server.jks -storepass password
4、查看證書內容
keytool -list -v -keystore server.jks -storepass password
5、配置tomcat service.xml文件
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="true" sslProtocol="TLS"
keystoreFile="D:/server.jks" keystorePass="password"
truststoreFile="D:/server.jks" truststorePass="password"
/>
clientAuth="true"表示雙向認證
6、導入客戶端證書到瀏覽器
雙向認證需要強制驗證客戶端證書。雙擊“custom.p12”即可將證書導入至IE
tomcat6配置單向認證
1、生成服務器端證書
keytool -genkey -keyalg RSA -dname "cn=localhost,ou=sango,o=none,l=china,st=beijing,c=cn" -alias server -keypass password -keystore server.jks -storepass password -validity 3650
2、由于是單向認證,沒有必要生成客戶端的證書,直接進入配置tomcat service.xml文件
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS"
keystoreFile="D:/server.jks" keystorePass="password"
/>
clientAuth="false"表示單向認證,同時去掉truststoreFile="D:/server.jks" truststorePass="password"這2
---The key thing to know is that IDs identify a specific element and therefore must be unique on the page – you can only use a specific ID once per document. Many browsers do not enforce this rule but it is a basic rule of HTML/XHTML and should be observed. Classes mark elements as members of a group and can be used multiple times, so if you want to define a style which will be applied to multiple elements you should use a class instead.
Notice that an ID's CSS is an HTML element, followed by a "#", and finally ID's name. The end result looks something like "element#idname". Also, be sure to absorb the fact that when an ID is used in HTML, we must use "id=name" instead of "class=name" to reference it!
ID = A person's Identification (ID) is unique to one person.
Class = There are many people in a class.
Standards specify that any given ID name can only be referenced once within a page or document. From our experience, IDs are most commonly used correctly in CSS layouts. This makes sense because there are usually only one menu per page, one banner, and usually only one content pane.
In Tizag.com CSS Layout Examples we have used IDs for the unique items mentioned above. View the CSS Code for our first layout example. Below are the unique IDs in our code.
Menu - div#menuPane
Content - div#content
Use IDs when there is only one occurence per page. Use classes when there are one or more occurences per page.
--Spring的singleton是容器級的,我們一般說的singleton模式是JVM級的。所以singleton模式中,singleton的class在整個JVM中只有一個instance,Spring的Bean,你可以一個class配置多個Bean,這個class就有了多個instance。這個singleton是指在spring容器中,這個Bean是單實例的,是線程共享的。所以要求這些類都是線程安全的。也就是說,不能出現修改Bean屬性的方法,當然除了設值得那些setter。只要滿足線程安全,這些bean都可以用singleton。而且我們在絕大多數使用上,也是這樣用的,包括dao,service。
這段代碼在單元測試的時候不會用任何問題,但是在多用戶測試的情況下,你會發現任何調用SomeService里someMethod()方法
的userID和userName都是同一個人,也就是第一個登陸的人的信息。Why?
其根本原因是Spring的Bean在默認情況下是Singleton的,Bean SomeServece的實例只會生成一份,也就是所SomeServece實例的user
對象只會被初始化一次,就是第一次登陸人的信息,以后不會變了。所以BaseService想為開發提供方便,卻給開發帶來了風險
正確的用法應該是這樣的
Interview Questions on UML and Design Patterns
Why to use design patterns?
Give examples of design patterns?
What is UML?
What are advantages of using UML?
What is the need for modelling?
Is it requiste to use UML in software projects?
What are use cases? How did you capture use cases in your project?
Explain the different types of UML diagrams ? sequence diagram , colloboration diagram etc
What is the sequence of UML diagrams in project?
What tools did you use for UML in your project?
What is the difference between activity and sequence diagrams?
What are deployment diagrams?
What are the different object relationships ?
What is the difference between composition and aggregation?
Wheel acting as a part of car ? Is this composition or aggregation?
面是自己自動調度的一些學習。
這里只采用jdk自帶的timer進行的,準備在下篇文章中用Quartz調度器。
首先建立你自己要運行的類。
package com.duduli.li;
public class Display {
public void disp(){
System.out.println("自動控制測試");
}
}
一個簡單的java bean,其中在這里你可以替換自己的任務。
然后就是編寫調度程序,這里要繼承jdk中的TimerTask類,復寫他的run方法。
package com.duduli.li;
import java.util.TimerTask;
public class AutoRan extends TimerTask {
//set方法是spring的DI
private Display display;
public void setDisplay(Display display) {
this.display = display;
}
@Override
public void run() {
display.disp();
}
}
然后就是重要的一步,編寫applicationsContext.xml了。
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="display"
class="com.duduli.li.Display">
</bean>
<bean id="atuoRun"
class="com.duduli.li.AutoRan">
<property name="display" ref="display"></property>
</bean>
<bean id="aR"
class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="timerTask" ref="atuoRun"></property>
<!--
period多長時間運行一次,delay表示允許你當任務第一次運行前應該等待多久
-->
<property name="period" value="5000"></property>
<property name="delay" value="2000"></property>
</bean>
<bean id="test"
class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list>
<!--
這里使用list,可以調度多個bean,
-->
<ref bean="aR"/>
</list>
</property>
</bean>
</beans>
再來就是客戶端調度了。
package com.duduli.li;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Client {
public static void main(String[] args) {
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
factory.getBean("test");
}
}
使用quartz和spring自動調度。
具體實現bean:
package com.duduli.li.quartz;
import java.util.Date;
public class Display {
@SuppressWarnings("deprecation")
public void disp(){
System.out.println(new Date().getSeconds());
System.out.println("自動控制測試");
}
}
繼承quartzjobbean類:這個類和繼承Timer類類似
。
package com.duduli.li.quartz;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class AutoRun extends QuartzJobBean{
private Display display;
public void setDisplay(Display display) {
this.display = display;
}
@Override
protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException {
display.disp();
}
}
spring配置文件:
<!-- quartz進行自動調度 -->
<!-- 具體實現類 -->
<bean id="display2" class="com.duduli.li.quartz.Display"></bean>
<!-- spring對quartz的支持,Auto類實現quartz的job接口的類,jobDataAsMap是將實現類注入其中 -->
<bean id="quartz" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.duduli.li.quartz.AutoRun"/>
<property name="jobDataAsMap">
<map>
<entry key="display" value-ref="display2"></entry>
</map>
</property>
</bean>
<!-- spring對quartz的支持,對其值的設定 -->
<bean id="simpleTask" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="quartz"></property>
<property name="startDelay" value="2000"></property>
<property name="repeatInterval" value="2000"></property>
</bean>
<!-- 啟動自動調度 -->
<bean id="quartzTest" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="simpleTask"/>
</list>
</property>
</bean>
client調用:
package com.duduli.li.quartz;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Client {
public static void main(String[] args) {
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
factory.getBean("quartzTest");
}
}
Java implements a very efficient interprocess communication which reduces the CPU’s idle time to a very great extent. It is been implemented through wait ( ), notify ( ) and notifyAll ( ) methods. Since these methods are implemented as final methods they are present in all the classes.
The basic functionality of each one of them is as under:
■ wait( ) acts as a intimation to the calling thread to give up the monitor and go to sleep until some other thread enters the same monitor and calls notify( ).
■ notify( ) is used as intimator to wake up the first thread that called wait( ) on the same object.
■ notifyAll( ) as the term states wakes up all the threads that called wait( ) on the same object. The highest priority thread will run first.
public class WaitNotifyAllExample { public static void main(String[] args) { try { Object o = new Object(); Thread thread1 = new Thread(new MyOwnRunnable("A", o)); Thread thread2 = new Thread(new MyOwnRunnable("B", o)); Thread thread3 = new Thread(new MyOwnRunnable("C", o)); // synchronized keyword acquires lock on the object. synchronized (o) { thread1.start(); // wait till the first thread completes execution. // thread should acquire the lock on the object // before calling wait method on it. Otherwise it will // throw java.lang.IllegalMonitorStateException o.wait(); thread2.start(); // wait till the second thread completes execution o.wait(); thread3.start(); } } catch (InterruptedException e) { e.printStackTrace(); } } } class MyOwnRunnable implements Runnable { private String threadName; private Object o; public MyOwnRunnable(String name, Object o) { threadName = name; this.o = o; } public void run() { synchronized (o) { for (int i = 0; i < 1000; i++) { System.out.println("Thread " + threadName + " Count : " + i); } // notify all threads waiting for the object o. // thread should acquire the lock on the object // before calling notify or notifyAll method on it. // Otherwise it will throw java.lang.IllegalMonitorStateException o.notifyAll(); } } }只有注冊用戶登錄后才能閱讀該文。閱讀全文
What are JavaScript types? - Number, String, Boolean, Function, Object, Null, Undefined.
A SQL profile is sort of like gathering statistics on A QUERY - which involves many tables, columns and the like.... In fact - it is just like gathering statistics for a query, it stores additional information in the dictionary which the optimizer uses at optimization time to determine the correct plan. The SQL Profile is not "locking a plan in place", but rather giving the optimizer yet more bits of information it can use to get the right plan.