如何使用JSP/Servlet将文件上传到服务器?我试过这个:
<form action="upload" method="post">
<input type="text" name="description" />
<input type="file" name="file" />
<input type="submit" />
</form>
Run Code Online (Sandbox Code Playgroud)
但是,我只获取文件名,而不是文件内容.当我添加 enctype="multipart/form-data"
到<form>
,然后request.getParameter()
返回null
.
在研究期间,我偶然发现了Apache Common FileUpload.我试过这个:
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = upload.parseRequest(request); // This line is where it died.
Run Code Online (Sandbox Code Playgroud)
不幸的是,servlet抛出了一个没有明确消息和原因的异常.这是堆栈跟踪:
SEVERE: Servlet.service() for servlet UploadServlet threw exception
javax.servlet.ServletException: Servlet execution threw an exception
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:313)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at …
Run Code Online (Sandbox Code Playgroud) 我已经开发了一个将信息发送到Servlet的HTML页面.在Servlet中,我使用的方法doGet()
和doPost()
:
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String id = req.getParameter("realname");
String password = req.getParameter("mypassword");
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String id = req.getParameter("realname");
String password = req.getParameter("mypassword");
}
Run Code Online (Sandbox Code Playgroud)
在调用Servlet的html页面代码中:
<form action="identification" method="post" enctype="multipart/form-data">
User Name: <input type="text" name="realname">
Password: <input type="password" name="mypassword">
<input type="submit" value="Identification">
</form>
Run Code Online (Sandbox Code Playgroud)
当我method = "get"
在Servlet中使用时,我获得了id和password的值,但是在使用时method = "post"
,id和password被设置为null
.为什么我不能在这种情况下获得值?
我想知道的另一件事是如何使用Servlet生成或验证的数据.例如,如果上面显示的Servlet对用户进行身份验证,我想在HTML页面中打印用户ID.我应该能够将字符串'id'作为响应发送,并在我的HTML页面中使用此信息.可能吗?
我的文件WebContent/jsps
夹中的JSP文件中有一个HTML表单.我servlet.java
在src
文件夹中的默认包中有一个servlet类.在我web.xml
的映射为/servlet
.
我action
在HTML表单的属性中尝试了几个URL :
<form action="/servlet">
Run Code Online (Sandbox Code Playgroud)
<form action="/servlet.java">
Run Code Online (Sandbox Code Playgroud)
<form action="/src/servlet.java">
Run Code Online (Sandbox Code Playgroud)
<form action="../servlet.java">
Run Code Online (Sandbox Code Playgroud)
但这些都不起作用.他们都在Tomcat 6/7/8中继续返回如下所示的HTTP 404错误:
HTTP状态404 - /servlet
描述:请求的资源(/ servlet)不可用.
或者如下面的Tomcat 8.5/9:
HTTP状态404 - 未找到
消息:/ servlet
描述:源服务器没有找到目标资源的当前表示,或者不愿意透露存在该资源
为什么不起作用?
我正在使用JSP和JDBC实现MVC.我已将数据库类文件导入到我的JSP文件中,并且我想显示数据库表的数据.我不知道应该如何ResultSet
从Java类返回到JSP页面并将其嵌入HTML中.
我怎样才能做到这一点?
我的Java文件是:
public class MyClass {
public void method1() {
// some code
}
public void method2() {
//some code
}
public void method3() {
//some code
}
}
Run Code Online (Sandbox Code Playgroud)
在我的JSP页面中,我有三个HTML按钮.
如果我点击button1
,那么只会method1
被调用,如果我点击,button2
那么只会method2
执行,如果button3
,然后只有method3
,依此类推.
我怎样才能做到这一点?
我想调用一个返回值的javascript函数,然后将该值放在if语句中.HTML中有两个单选按钮,javascript会检查单击哪一个.之后,JSP将其与"客户"或"公司"进行比较,并执行相应的SQL查询.
使用Javascript:
function corc{
var value;
if(document.getElementById('cust').checked){
value='customer';
return value;
}else if(document.getElmentById('comp').checked){
value='company';
return value;
}
}
Run Code Online (Sandbox Code Playgroud)
JSP:
if(%>corc();<%.equals("customer")){
String sqlqueryCommand = "SELECT * from customer where login='" + v1 + "' and password='" + v2 + "'";
}else if (%>corc();<%.equals("company")){
String sqlqueryCommand = "SELECT * from company where login='" + v1 + "' and password='" + v2 + "'";
}
Run Code Online (Sandbox Code Playgroud) 如何使用jstl获取/设置复选框值并仅删除选中复选框的数据库中的那些记录?您是否还可以建议如何在jstl中使用三元运算符?
SearchStudent.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Lookup Students</title>
</head>
<form method="post" action="deleteStudentServlet" class="form">
<body class="body">
<!-- List results -->
<c:if test="${not empty studentList}">
<table border="1" cellspacing="0" cellpadding="0" :>
<tr>
<th></th>
<th>ID</th>
<th>Title</th>
<th>First Name</th>
<th>Last Name</th>
<th></th>
</tr>
<c:forEach var="students" items="${studentList}">
<tr>
<td><input type="checkbox" name="chkBox"> </td>
<td>${students.studentID}</td>
<td>${students.title}</td>
<td>${students.firstName}</td>
<td>${students.lastName}</td>
<td><c:url value="UDS" var="url">
<c:param name="StudentID" value="${students.studentID}" />
</c:url> <a href="${url}">Edit</a></td>
</tr>
</c:forEach>
</table>
</c:if>
<td><input …
Run Code Online (Sandbox Code Playgroud) 在从HTML提交数据后,servlet将这些数据添加到我的数据库并将结果消息转发到JSP页面.我希望在转发之后保留表单中最初提交的值.
在servlet中创建一个对象并添加我收到的所有参数并向JSP发送请求是否明智?还有另一种更好的方法吗?
当用户提交错误的输入时,如何在同一个JSP中显示错误消息?我不打算抛出异常并显示错误页面.
jsp ×8
servlets ×7
java ×3
forms ×2
html ×2
button ×1
checkbox ×1
file-upload ×1
java-ee ×1
javascript ×1
jdbc ×1
jstl ×1
methods ×1
retain ×1
validation ×1