Advanced Java Q and A
Java Swing is a part of Java's GUI toolkit that allows the creation of window-based applications. It's built on top of AWT (Abstract Window Toolkit) and provides lightweight components such as JButton, JLabel, and JTable.
| Feature | AWT | Swing |
|---|---|---|
| Platform Dependency | Dependent (uses native OS) | Platform independent |
| Components | Heavyweight | Lightweight |
| Look and Feel | Fixed (OS-based) | Pluggable (customizable) |
JFrame is the top-level container used to create a window in Swing. It provides a title bar, close/minimize buttons, and a content pane to hold other components.
Using JOptionPane.showMessageDialog(null, "Hello World!");
A layout manager controls the positioning and sizing of components in a container, e.g.:
It displays data in a tabular form (rows and columns) and supports editing, sorting, and custom rendering.
By calling: UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
JDBC is an API that enables Java applications to interact with relational databases using SQL.
A precompiled SQL statement that improves performance and prevents SQL injection.
PreparedStatement ps = con.prepareStatement("SELECT * FROM users WHERE id=?");
ps.setInt(1, 101);
con.setAutoCommit(false);
try {
// SQL operations
con.commit();
} catch(Exception e) {
con.rollback();
}
| Feature | Statement | PreparedStatement |
|---|---|---|
| Compilation | Compiled each time | Compiled once |
| Performance | Slower | Faster |
| Security | Vulnerable to SQL injection | Secure |
while(rs.next()) {
System.out.println(rs.getString("name"));
}
A Servlet is a Java class that handles HTTP requests and responses on a web server, extending the capabilities of a web application.
| Feature | doGet() | doPost() |
|---|---|---|
| Data in URL | Visible | Hidden |
| Data limit | Limited (~2KB) | No limit |
| Use case | Retrieve data | Submit data |
String name = request.getParameter("username");
It is the deployment descriptor that defines servlet mappings, initialization parameters, and configuration details.
It represents the web application environment and allows servlets to share information (e.g., application-wide parameters).
Using: response.sendRedirect("NextServlet");
JSP is a server-side technology that allows embedding Java code inside HTML to create dynamic web pages.
| Feature | JSP | Servlet |
|---|---|---|
| Syntax | HTML + Java | Pure Java |
| Use | Presentation layer | Logic layer |
| Compilation | Auto-translated to Servlet | Manually written |
They give global information about the JSP page. Example: <%@ page language="java" contentType="text/html" %> <%@ include file="header.jsp" %>
JSP provides 9 implicit objects automatically, e.g.: request, response, session, application, out, page, pageContext, config, exception.
<jsp:include page="footer.jsp" />
| Feature | <jsp:include> | <%@ include %> |
|---|---|---|
| Type | Dynamic | Static |
| Evaluation | Runtime | Translation time |
EL simplifies access to data stored in JavaBeans, request, session, etc.
Example:
${user.name} instead of <%= user.getName() %>
<jsp:forward page="next.jsp" />