我刚开始使用Servlets/JSP/JSTL,我有类似这样的东西:
<html>
<body>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<jsp:directive.page contentType="text/html; charset=UTF-8" />
<c:choose>
<c:when test='${!empty login}'>
zalogowany
</c:when>
<c:otherwise>
<c:if test='${showWarning == "yes"}'>
<b>Wrong user/password</b>
</c:if>
<form action="Hai" method="post">
login<br/>
<input type="text" name="login"/><br/>
password<br/>
<input type="password" name="password"/>
<input type="submit"/>
</form>
</c:otherwise>
</c:choose>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
并在我的doPost方法
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
HttpSession session=request.getSession();
try
{
logUser(request);
}
catch(EmptyFieldException e)
{
session.setAttribute("showWarning", "yes");
} catch (WrongUserException e)
{
session.setAttribute("showWarning", "yes");
}
RequestDispatcher d=request.getRequestDispatcher("/index.jsp");
System.out.println("z");
d.forward(request, response);
}
Run Code Online (Sandbox Code Playgroud)
但有些东西不起作用,因为我想要这样的东西:
我想知道这些servlet方法.我对这些方法有所了解
doPost对于参数数字没有限制doGet.doGet比...更快doPost.doPost是安全的doGet.我的问题是,因为这些方法采用相同的参数并完成我们实施的过程.那么major difference这些方法和之间的关系是什么At which specific situation each of this method is used to process.
流量的差异,我知道doGet()是预处理,而dopost是后处理,但那是什么?
我有jsp页面 -
<html>
<head>
</head>
<body>
<%
String valueToPass = "Hello" ;
%>
<a href="goToServlet...">Go to servlet</a>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
和servlet -
@WebServlet(name="/servlet123",
urlPatterns={"/servlet123"})
public class servlet123 extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
}
public void foo() {
}
}
Run Code Online (Sandbox Code Playgroud)
<a href="goToServlet...">Go to servlet</a>为了传递值(比如valueToPass或者可能将值作为参数添加到),我应该写什么servlet123?
我可以使用jsp中的链接调用servlet123(例如foo())中的特定方法吗?
编辑:
如何在URL中调用servlet?我的页面层次结构如下 -
WebContent
|-- JSPtest
| |-- callServletFromLink.jsp
|-- WEB-INF
: :
Run Code Online (Sandbox Code Playgroud)
我想调用servlet123文件夹src-> control.
我试过<a href="servlet123">Go …
我创建了一个JSP文件.
sample.jsp
<%@ page pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
This is jsp program
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我把它放在samplejsp项目中.
samplejsp
`-- WebContent
`-- WEB-INF
`-- sample.jsp
Run Code Online (Sandbox Code Playgroud)
我在以下网址上打开了它.
http://localhost:8080/samplejsp/sample.jsp
但它在浏览器中显示以下错误.
404错误
请求的资源(/sample.jsp)不可用.
我可以在不使用HTML表单的情况下从JSP文件调用servlet吗?
例如,在页面加载期间显示HTML表格中的数据库结果.
我想知道在servlet中为什么我们在同一个程序中一起使用doGet和doPost方法.有什么用?
以下代码的含义是什么?
为什么要从doPost调用doGet方法?我对这段代码一点也不清楚.
public class Info extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
doGet(request, response);
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢
下面的代码来自一本书,所以它不会出错.但我不知道如何解决这个错误.当删除方法doGet()时,同样的错误!
"HTTP状态405 - 此URL不支持HTTP方法GET"
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class PDFServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException{
this.doPost(request,response);
}
@Override
protected void doPost(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException{
response.setContentType("application/pdf");
ServletOutputStream out=response.getOutputStream();
File pdf=null;
BufferedInputStream buf=null;
try{
pdf=new File("C:\\Users\\lk\\Desktop\\Desktop\\ example.pdf");
response.setContentLength((int)pdf.length());
FileInputStream input=new FileInputStream(pdf);
buf=new BufferedInputStream(input);
int readBytes=0;
while((readBytes=buf.read())!=-1) out.write(readBytes);
}catch(IOException e){
System.out.println("file not found!");
}finally{
if(out!=null) …Run Code Online (Sandbox Code Playgroud) 我有一个HTML表,其中包含从该表中显示的数据库中提取的行.我希望用户能够通过单击每行之外的删除超链接或按钮来删除行.
当用户点击每个删除超链接或按钮时,如何在页面上调用JSP函数,以便我可以从数据库中删除该行的条目?<a>or <button>标签究竟应该调用JSP函数到底是什么?
请注意,我需要调用JSP函数,而不是JavaScript函数.
我有一个flights_DB包含一个名为的表的数据库Passengers.每位乘客都由他的护照号码唯一标识.
我想创建一个包含表格中所有护照号码的下拉列表Passengers.如何使用JSP和Servlets实现这一目标?
在用于Java的HTTP servlet中,我想提交一个表单.从输入字段中携带值的推荐方法是什么?它是使用隐藏的字段并通过request.getParameter(...)或与他们通过request.getAttribute(...)?