我是Java EE的新手,我知道类似以下三行
<%= x+1 %>
<%= request.getParameter("name") %>
<%! counter++; %>
Run Code Online (Sandbox Code Playgroud)
是一种旧式的编码方式,在JSP版本2中,存在一种避免JSP文件中的Java代码的方法.有人可以告诉我替代的JSP 2行,以及这种技术的名称是什么?
假设,我有一个拥有大量servlet的Web服务器.对于在这些servlet之间传递的信息,我正在设置会话和实例变量.
现在,如果有2个或更多用户向此服务器发送请求,那么会话变量会发生什么?它们对所有用户都是通用的,或者对于每个用户而言都是不同的.如果它们不同,那么服务器如何区分不同的用户?
还有一个类似的问题,如果有n用户访问特定的servlet,那么这个servlet只在第一个用户第一次访问它时实例化,或者是否为所有用户单独实例化?换句话说,实例变量会发生什么?
java multithreading servlets session-variables instance-variables
JSP和Servlet如何相互关联?JSP是某种Servlet吗?JSP和JSF如何相互关联?JSF是一种基于预构建UI的JSP,如ASP.NET-MVC吗?
我是Web应用程序和Servlet的新手,我有以下问题:
每当我在servlet中打印一些东西并通过webbrowser调用它时,它就会返回一个包含该文本的新页面.有没有办法使用Ajax在当前页面中打印文本?
我已经开发了一个将信息发送到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中.
我怎样才能做到这一点?
我有一个带有HTML表单的JSP页面:
<form action="SERVLET">
<input type="text" name="name"/><br>
<input type="text" name="group"/>
<input type="text" name="pass"/>
<input type="submit" value="submit">
</form>
Run Code Online (Sandbox Code Playgroud)
如何在servlet中获取这些数据并将它们添加到数据库中?
这可以返回一个字符串:
import javax.servlet.http.*;
@SuppressWarnings("serial")
public class MonkeyServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/plain");
resp.getWriter().println("got this far");
}
}
Run Code Online (Sandbox Code Playgroud)
但我无法让它返回一个HTML文档.这不起作用:
import javax.servlet.http.*;
@SuppressWarnings("serial")
public class BlotServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/html");
resp.getWriter().println("html/mypage.html");
}
}
Run Code Online (Sandbox Code Playgroud)
对不起是菜鸟!
编辑:
我已经在单独的文档中有html.所以我需要返回文档,或以某种方式读取/解析它,所以我不只是重新输入所有的HTML ...
编辑:
我在我的web.xml中有这个
<servlet>
<servlet-name>Monkey</servlet-name>
<servlet-class>com.self.edu.MonkeyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Monkey</servlet-name>
<url-pattern>/monkey</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
还有什么我可以放在那里所以它只返回一个文件,如...
<servlet-mapping>
<servlet-name>Monkey</servlet-name>
<file-to-return>blot.html</file-to-return>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)