Back to Java
2026-05-156 min read

Contenteditable Border (Java)

Learn Contenteditable Border (Java) step by step with clear examples and exercises.

Title: Contenteditable Border (Java) - A full guide

Why This Matters

In web development, content editable elements allow users to modify the content directly within a webpage. However, managing borders for these elements can be tricky, especially in Java. Understanding how to set and remove contenteditable border is essential for creating interactive and user-friendly websites. This lesson will guide you through the process with practical examples and tips.

Content editable elements are crucial for creating dynamic web applications where users can interact directly with the page's content. By learning how to control the contenteditable border, you can create a more visually appealing and engaging user experience.

Prerequisites

To follow this tutorial, you should have a basic understanding of:

  1. HTML: Understanding the structure of an HTML document, including content editable elements (contenteditable="true").
  2. CSS: Familiarity with CSS properties and selectors to style your webpage.
  3. Java: Basic knowledge of Java programming language, including creating a simple Java Web Application using Servlets and JSP.
  4. JavaScript: Understanding the basics of JavaScript to handle user interactions and manipulate the DOM.

Core Concept

To set or remove the contenteditable border in Java, you'll need to combine HTML, CSS, JavaScript, and Java code. The primary goal is to create an interactive webpage with a content editable element that has a customizable border.

  1. Create an HTML file (index.html) with a content editable div:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Contenteditable Border</title>
<!-- Include your custom CSS file -->
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div id="contentEditable" contenteditable="true"></div>
<!-- Include your custom JavaScript file -->
<script src="scripts.js"></script>
</body>
</html>
  1. Create a CSS file (styles.css) to style the content editable element:
#contentEditable {
border: 1px solid black;
padding: 10px;
min-height: 200px;
}
  1. Create a Java Servlet (ContenteditableServlet.java) to handle the HTTP request and response:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class ContenteditableServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}

private void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();

// Set the contenteditable border here
String borderColor = "black";
String borderWidth = "1px";

out.println("<!DOCTYPE html>");
out.println("<html lang=\"en\">");
out.println("<head>");
out.println(" <meta charset=\"UTF-8\">");
out.println(" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">");
out.println(" <title>Contenteditable Border</title>");
out.println(" <!-- Include your custom CSS file -->");
out.println(" <style>");
out.println(" #contentEditable {");
out.println(" border-color: " + borderColor + ";");
out.println(" border-width: " + borderWidth + ";");
out.println(" padding: 10px;");
out.println(" min-height: 200px;");
out.println(" }");
out.println(" </style>");
out.println("</head>");
out.println("<body>");
out.println(" <div id=\"contentEditable\" contenteditable=\"true\"></div>");
out.println(" <!-- Include your custom JavaScript file -->");
out.println("</body>");
out.println("</html>");
}
}
  1. Create a Deployment Descriptor (web.xml) to map the Servlet:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>ContenteditableServlet</servlet-name>
<servlet-class>ContenteditableServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ContenteditableServlet</servlet-name>
<url-pattern>/index.html</url-pattern>
</servlet-mapping>
</web-app>

Now, when you run this Java Web Application, the content editable element will have a black border with 1px width. You can modify the borderColor and borderWidth variables in the Servlet to change the border properties dynamically.

Worked Example

To set different borders for the content editable element based on user input, create an HTML form and a JavaScript function:

  1. Modify the index.html file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Contenteditable Border</title>
<!-- Include your custom CSS file -->
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div id="contentEditable" contenteditable="true"></div>

<!-- Add a form to change the border properties -->
<form id="borderForm">
<label for="borderColor">Border Color:</label>
<input type="color" id="borderColor" name="borderColor" value="#000000">
<br>
<label for="borderWidth">Border Width:</label>
<input type="text" id="borderWidth" name="borderWidth" value="1px">
<br>
<button onclick="setContentEditableBorder()">Set Border</button>
</form>

<!-- Include your custom JavaScript file -->
<script src="scripts.js"></script>
</body>
</html>
  1. Create a JavaScript file (scripts.js) to handle the form submission and set the contenteditable border:
function setContentEditableBorder() {
// Get user input values
const borderColor = document.getElementById("borderColor").value;
const borderWidth = document.getElementById("borderWidth").value;

// Update the CSS in the head of the HTML document
const styleElement = document.createElement("style");
styleElement.innerHTML = `
#contentEditable {
border-color: ${borderColor};
border-width: ${borderWidth};
}
`;
document.head.appendChild(styleElement);
}

Now, when you run the Java Web Application and fill out the form, the content editable element's border will be updated according to your input.

Common Mistakes

  1. Forgetting to include the custom CSS file in the HTML head:
<!-- Include your custom CSS file incorrectly -->
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Contenteditable Border</title>
<!-- Include your custom CSS file in the body instead -->
<link rel="stylesheet" href="styles.css">
</head>
  1. Not updating the content editable border when the form is submitted:
function setContentEditableBorder() {
// Get user input values
const borderColor = document.getElementById("borderColor").value;
const borderWidth = document.getElementById("borderWidth").value;

// This line is missing: update the CSS in the head of the HTML document
// const styleElement = document.createElement("style");
// styleElement.innerHTML = `
// #contentEditable {
// border-color: ${borderColor};
// border-width: ${borderWidth};
// }
// `;
// document.head.appendChild(styleElement);
}
  1. Not handling the case where the user submits the form without filling out the input fields:
function setContentEditableBorder() {
const borderColor = document.getElementById("borderColor").value;
const borderWidth = document.getElementById("borderWidth").value;

if (!borderColor || !borderWidth) {
alert("Please fill out both form fields.");
return;
}

// Update the CSS in the head of the HTML document
const styleElement = document.createElement("style");
styleElement.innerHTML = `
#contentEditable {
border-color: ${borderColor};
border-width: ${borderWidth};
}
`;
document.head.appendChild(styleElement);
}

Practice Questions

  1. Modify the Java Servlet to allow users to set different background colors for the content editable element based on user input.
  2. Create a JavaScript function that allows users to change the padding of the content editable element dynamically.
  3. Implement a feature that saves the current border properties (color, width, and padding) when the user clicks outside the content editable area.
  4. Add validation to the form to ensure that only numbers are entered for the border width.

FAQ

Q: Why can't I see the changes to the content editable border immediately after updating the form?

A: The browser needs to re-render the page for the updated CSS to take effect. To avoid this, you can use JavaScript to update the CSS in the head of the HTML document instead of relying on the server-side code.

Q: How can I make the content editable border responsive, so it adapts to different screen sizes?

A: You can use media queries in your custom CSS file to set different borders for various screen sizes. For example:

@media (max-width: 600px) {
#contentEditable {
border-width: 0.5px;
}
}

Q: How can I save the current border properties when the user clicks outside the content editable area?

A: You can use JavaScript to listen for click events on the document and store the current border properties in a cookie or local storage. When the form is submitted, you can retrieve these stored values and apply them to the contenteditable element.

Q: How can I validate the border width input field to ensure only numbers are entered?

A: You can use regular expressions (regex) in JavaScript to validate the input field. Here's an example using a regex that matches any number followed by an optional unit (px or pt):

const borderWidthInput = document.getElementById("borderWidth");
borderWidthInput.addEventListener("input", function(event) {
const inputValue = event.target.value;
const regex = /^(\d+(\.\d*)?)(px|pt)?$/;
if (!regex.test(inputValue)) {
alert("Please enter a valid number followed by an optional unit (px or pt).");
event.target.value = "";
}
});
Contenteditable Border (Java) | Java | XQA Learn