Sun River
Topics about Java SE, Servlet/JSP, JDBC, MultiThread, UML, Design Pattern, CSS, JavaScript, Maven, JBoss, Tomcat, ... |
Question 1:
You need to create a database connection in your application after reading the username, password, and database server URL from the deployment descriptor. Which will be the best place to do this?
Choices:
Correct choice:
Explanation:
The init() method is invoked once and only once by the container, so the creation of the database connection will be done only once, which is appropriate. The service() , doGet() , and doPost() methods might be called many times by the container.
The username, password, and URL are to be read from the deployment descriptor. These initialization parameters are contained in the ServletConfig object, which is passed to the init() method. That is why we need to use the init() method instead of the constructor for this purpose, even though the constructor is also called only once.
Question 2:
A user can select multiple locations from a list box on an HTML form. Which of the following methods can be used to retrieve all the selected locations?
Choices:
Correct choice:
Explanation:
The getParameterValues(String paraName) method of the ServletRequest interface returns all the values associated with a parameter. It returns an array of Strings containing the values. The getParameter() method returns just one of the values associated with the given parameter, so choice A is incorrect. There are no methods named getParameters() or getParamValues() , so choices B and D are incorrect.
Question 1:Consider the following servlet code:
public class MyServlet extends HttpServletWhich of the following variables in the above code are thread safe?
Choices:
Correct choices:
Explanation:
The static variable i is thread safe because it is final (cannot be modified), or else it would not have been safe. Request and response objects are scoped only for the lifetime of the request, so they are also thread safe. Session and ServletContext objects can be accessed from multiple threads while processing multiple requests, so they are not thread safe. However, in this case, the ServletContext object is synchronized, so it can be accessed only by one thread at a time. obj is not thread safe because even though the ServletContext object is synchronized, its attributes are not. They need to be synchronized separately. Hence choices B and E are incorrect and choices A, C, D and F are correct.
Question 2:
Which of the following statements are true?
Choices:
Correct choices:
Explanation:
When SingleThreadModel is implemented, the servlet container ensures that only one thread is executing the servlet's method at a time. So what will happen for multiple requests? In that case, the container may instantiate multiple instances of the servlet to handle multiple requests, so option A is correct and B is incorrect.
If the SingleThreadModel interface is not implemented, a servlet uses the multi-threaded model (that is, multiple threads can access the methods of the servlet). Static variables can be accessed through multiple instances of the same class, so they are not always thread safe. Hence choices B and C are incorrect and choices A and D are correct.
Ajax isn’t a technology. It’s really several technologies, each flourishing in its own right, coming together in powerful new ways. Ajax incorporates:
Questions:
- Entry level:
? - Is AJAX a programming language?
? - What is AJAX?
? - How new is AJAX?
? - Why can/should AJAX be used?
?A: AJAX is best suited for small (hopefully unobtrusive) updates to the current
? ? web page, based on information that is not available until it has been provided
? ? by the end user.
? - When should AJAX NOT be used?
? A: It would not be appropriate to use AJAX when the "answer/result" can be determinded
? ? by the client. ?Generally, the purpose of AJAX is to submit a short request to the server,
? ? and process the response in such a way as to add value to the currently displayed page.
? ? It would also not be appropriate to use AJAX when the magnitude of the response is such
? ? that it would be easier, and more clear to redisplay the page.
? - What objects are used by AJAX programs?
- Intermediate-level?
? - Describe the ?formats and protocols used/specified by AJAX
? - Describe some things that can't be done with AJAX
?A: Sending a request to a server outside of the domain from which? the web page originated.
? - How should AJAX objects be created?
? - For what error conditions should programs check?
? - Are Finite State Machines (FSM's) appropriate for use with AJAX?
? - Identify and describe the state transitions that can/should occur within a transaction
A: - Reset : When the XmlHttpRequest object is created, no connection yet exists between the clent, and the server.
? ? ? Open ?: When the xmlHttp.open() is issued, the request is being prepared for transmission to the server
? ? ? Sent ? : When the xmlHttp.send() is issued, the request is transmitted to the server application
? ? ? Rcvd ? : When the xmlHttp callback routine is called, the readyState and status fields of the object define why the routine was called
Q. How do you know that an AJAX request has completed?
A. The XHR.readyState is 4 and the XHR.status is 200 (or zero if the request is to a local file). The callback function is called four times - first with status=1, then 2,3, and finally 4.
Q. How does XML processing differ on the different browsers?
A. It's an ActiveX object on IE, but is native on the other browsers
Q: What values exists for the XmlHttpRequest.readyState field, and what do they mean?
A: readyState values:
? ? 0 = uninitialized
? ? 1 = loading
? ? 2 = loaded
? ? 3 = interactive
? ? 4 = complete??
Other areas to check up on:
? ?How do you process the returned XML data?
? ?If it's a Java/J2EE place: what about AJAX and JSF?
? ?How to populate the XML response on the server?
? ?How to terminate an active request?
What are the design patterns in Java?
In j2ee they have 15 Design Pattern:
Presentation Tier:
1.Intercepting Filter
2. Front Controller
3. View Helper
4. Composite View
5. Service to Worker
6. Dispatcher View
Business Tier:
7. Business Delegate
8. Transfer Object
9. Session Facade
10. Service Locator
11. Transfer Object
12. Value List Handler
13. Composite Entity
Integration Tier:
14.Service Activator
15 Data Access Object
package own; import java.lang.reflect.*; import java.util.*; public class OptimizedReflectionMarshaller { // cache for getters private static HashMap gettersMap = new HashMap(); // cache for storing info on whether certain class implements Collection private static HashMap collectionsMap = new HashMap(); private static final String JAVA = "java."; private static final String JAVAX = "javax."; private static final Class[] EMPTYPARAMS = new Class[0]; /** * Info on a single field and the corresponding getter method */ private static class FieldMethodPair { private String fieldName; private Method getterMethod; public FieldMethodPair(String fieldName, Method getterMethod) { this.fieldName = fieldName; this.getterMethod = getterMethod; } public String getFieldName() { return fieldName; } public Method getGetterMethod() { return getterMethod; } } /** * Returns the marshalled XML representation of the parameter object */ public static String marshal(Object obj) { StringBuffer sb = new StringBuffer(); Class clazz = obj.getClass(); // get class name in lower letters (w/o package name) String className = clazz.getName(); int lastDotIndex = className.lastIndexOf("."); if (lastDotIndex >= 0) className = className.substring(lastDotIndex + 1); className = className.toLowerCase(); sb.append("<" + className + ">"); marshal(obj, sb); sb.append("</" + className + ">"); return sb.toString(); } /** * Returns getter function for the specified field */ private static Method getGetter(Class clazz, String fieldName) { try { // for example, for 'name' we will look for 'getName' String getMethodName = fieldName.substring(0, 1); getMethodName = getMethodName.toUpperCase(); getMethodName = "get" + getMethodName + fieldName.substring(1, fieldName.length()); Method getMethod = clazz.getMethod(getMethodName, EMPTYPARAMS); return getMethod; } catch (NoSuchMethodException nsme) { return null; } } /** * Returns a list of all fields that have getters */ private static List getAllGetters(Class clazz) { try { // check if have in cache if (gettersMap.containsKey(clazz.getName())) return (List) gettersMap.get(clazz.getName()); List fieldList = new LinkedList(); Class currClazz = clazz; while (true) { Field[] fields = currClazz.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field currField = fields[i]; int modifiers = currField.getModifiers(); // check if not static and has getter if (!Modifier.isStatic(modifiers)) { Method getterMethod = getGetter(clazz, currField .getName()); if (getterMethod != null) { FieldMethodPair fmp = new FieldMethodPair(currField .getName(), getterMethod); fieldList.add(fmp); } } } currClazz = currClazz.getSuperclass(); if (currClazz == null) break; } // store in cache gettersMap.put(clazz.getName(), fieldList); return fieldList; } catch (Exception exc) { exc.printStackTrace(); return null; } } /** * Checks whether the specified class implements Collection interface */ private static boolean isImplementsCollection(Class clazz) { String className = clazz.getName(); // check in cache if (collectionsMap.containsKey(className)) { return ((Boolean) collectionsMap.get(className)).booleanValue(); } boolean result = Collection.class.isAssignableFrom(clazz); // store in cache collectionsMap.put(className, new Boolean(result)); return result; } private static void appendFormatted(Object obj, StringBuffer sb) { String strRepresentation = obj.toString(); int len = strRepresentation.length(); for (int i = 0; i < len; i++) { char c = strRepresentation.charAt(i); switch (c) { case '&': sb.append("&"); break; case '<': sb.append("<"); break; case '>': sb.append(">"); break; case '\'': sb.append("'"); break; case '\"': sb.append("""); break; default: sb.append(c); } } } private static void marshal(Object obj, StringBuffer sb) { try { Class clazz = obj.getClass(); String className = clazz.getName(); // check if implements Collection if (isImplementsCollection(clazz)) { Collection cobj = (Collection) obj; Iterator it = cobj.iterator(); while (it.hasNext()) { Object eobj = it.next(); sb.append("<" + eobj.getClass().getName() + ">"); marshal(eobj, sb); sb.append("</" + eobj.getClass().getName() + ">"); } return; } // check for primitive types if (className.startsWith(JAVA) || className.startsWith(JAVAX)) { appendFormatted(obj, sb); return; } // otherwise put all fields with getters List allGetters = getAllGetters(clazz); Iterator itGetters = allGetters.iterator(); while (itGetters.hasNext()) { FieldMethodPair currGetter = (FieldMethodPair) itGetters.next(); String currFieldName = currGetter.fieldName; Method currentGetter = currGetter.getterMethod; // call method Object val = currentGetter.invoke(obj, EMPTYPARAMS); if (val != null) { sb.append("<" + currFieldName + ">"); // call recursively marshal(val, sb); sb.append("</" + currFieldName + ">"); } } } catch (Exception e) { e.printStackTrace(); } } }Feel free to use and modify.
RUP Contains phases, iterations, and workflows. ?
Has 4 phases:
????????????? 1). Inception--Define the scope of project
??????????????2). Elaboration-- Plan project, specify features, baseline architecture
????????????? 3). Construction--Build the product
????????????? 4). Transition--Transition the product into end user comminity
Use Case Driven, Designed and Documented using UML.
(From Ajaxprojects) |
||