jsp在input内添加文字
时间: 2024-02-24 08:56:59 浏览: 115
要在 `input` 标签内添加默认文字,可以使用 `placeholder` 属性,例如:
```html
<input type="text" placeholder="请输入您的用户名">
```
这样在输入框内就会显示灰色的默认文字 "请输入您的用户名",当用户开始输入时,该文字会自动消失。如果您想要在输入框内添加默认值而不是默认文字,可以使用 `value` 属性,例如:
```html
<input type="text" value="默认值">
```
这样在输入框内就会显示默认值 "默认值",用户可以直接编辑或者清空该值。
相关问题
jsp input鼠标悬浮提示文字
在jsp中,可以使用HTML的title属性来实现input鼠标悬浮提示文字。例如,假设有一个文本框需要添加提示文字,可以这样写:
```html
<input type="text" name="username" title="请输入用户名">
```
这样,当鼠标悬浮在文本框上时,会显示提示文字"请输入用户名"。需要注意的是,这种方式只适用于单行输入框,对于多行输入框(如textarea)则需要使用其他方式实现。
使用过滤器完成添加留言模块 insert.jsp:包含表单及非法文字提示 InsertMessage.java:接受表单参数,jdbc操作数据库 要求: 进行中文字符处理 如果提交内容中有非法字符串,将该字符串保存在request对象发送回insert.jsp,并显示““xxx”是非法文字”。(设定多个非法字符串) 如果登录非法,转发回insert.jsp。
下面是一个使用过滤器完成添加留言模块的示例代码:
InsertMessage.java:
```java
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class InsertMessage extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
String content = request.getParameter("content");
String ip = request.getRemoteAddr();
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = sdf.format(date);
Connection conn = null;
PreparedStatement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test?useSSL=false&characterEncoding=UTF-8";
String user = "root";
String password = "123456";
conn = DriverManager.getConnection(url, user, password);
String sql = "INSERT INTO messages(name, content, time, ip) VALUES (?, ?, ?, ?)";
stmt = conn.prepareStatement(sql);
stmt.setString(1, name);
stmt.setString(2, content);
stmt.setString(3, time);
stmt.setString(4, ip);
stmt.executeUpdate();
response.sendRedirect("list.jsp");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
```
InsertFilter.java:
```java
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
public class InsertFilter implements Filter {
private List<String> illegalWords;
public void init(FilterConfig config) throws ServletException {
String illegalWordsStr = config.getInitParameter("illegalWords");
illegalWords = Arrays.asList(illegalWordsStr.split(","));
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
String name = req.getParameter("name");
String content = req.getParameter("content");
for (String word : illegalWords) {
if (name.contains(word) || content.contains(word)) {
request.setAttribute("illegalWord", word);
req.getRequestDispatcher("insert.jsp").forward(request, response);
return;
}
}
chain.doFilter(request, response);
}
public void destroy() {
illegalWords = null;
}
}
```
在web.xml文件中配置过滤器:
```xml
<filter>
<filter-name>InsertFilter</filter-name>
<filter-class>InsertFilter</filter-class>
<init-param>
<param-name>illegalWords</param-name>
<param-value>非法1,非法2,非法3</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>InsertFilter</filter-name>
<url-pattern>/insert.jsp</url-pattern>
</filter-mapping>
```
在insert.jsp中添加表单和提示信息:
```html
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加留言</title>
</head>
<body>
<h1>添加留言</h1>
<form action="InsertMessage" method="post">
姓名:<input type="text" name="name"><br>
内容:<textarea name="content"></textarea><br>
<input type="submit" value="提交">
</form>
<%
String illegalWord = (String) request.getAttribute("illegalWord");
if (illegalWord != null) {
out.println("<p>" + illegalWord + "是非法文字</p>");
}
%>
</body>
</html>
```
在上面的代码中,`InsertFilter`过滤器用于过滤表单提交的内容,如果提交内容中包含非法字符串,则将该字符串保存在request对象中,并转发回insert.jsp页面,同时显示提示信息。如果提交内容合法,则继续执行后续操作。
阅读全文