我是Java EE的新手,我知道类似以下三行
<%= x+1 %>
<%= request.getParameter("name") %>
<%! counter++; %>
Run Code Online (Sandbox Code Playgroud)
是一种旧式的编码方式,在JSP版本2中,存在一种避免JSP文件中的Java代码的方法.有人可以告诉我替代的JSP 2行,以及这种技术的名称是什么?
我已经开发了一个将信息发送到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
描述:源服务器没有找到目标资源的当前表示,或者不愿意透露存在该资源
为什么不起作用?
在我的应用程序中,我有一个servlet,在web.xml中定义如下:
<servlet>
<display-name>Notification Servlet</display-name>
<servlet-name>NotificationServlet</servlet-name>
<servlet-class>com.XXX.servlet.NotificationServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>NotificationServlet</servlet-name>
<url-pattern>/notification/*</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
在使用Tomcat 7之后,我想使用@WebServlet将完成这项工作的注释.
这是我做的方式:
@WebServlet( name="NotificationServlet", displayName="Notification Servlet", urlPatterns = {"/notification"}, loadOnStartup=1)
public class NotificationServlet extends HttpServlet {
Run Code Online (Sandbox Code Playgroud)
它不起作用.有人可以告诉我我做错了什么吗?
我有以下servlet.我想在jsp页面加载时调用servlet .我怎样才能做到这一点?
servlet的: SomeServlet.java
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>SomeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
如何在jsp页面加载时编写相应的jsp来调用servlet.另外我需要从servlet获取结果并在同一个jsp中显示.我可以把结果寄回去jsp吗?
谢谢!
基本上,我想在JSP页面上的ArrayList中显示产品.我在servlet代码中完成了这个.但是没有输出.
我还必须将products.jsp放在/ WEB-INF文件夹中吗?当我这样做时,我得到一个请求的不是资源错误.
我的Servlet代码(InventoryServlet.java)
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
try {
List<Product> products = new ArrayList<Product>();
products = Inventory.populateProducts(); // Obtain all products.
request.setAttribute("products", products); // Store products in request scope.
request.getRequestDispatcher("/products.jsp").forward(request, response); // Forward to JSP page to display them in a HTML table.
} catch (Exception ex) {
throw new ServletException("Retrieving products failed!", ex);
}
}
Run Code Online (Sandbox Code Playgroud)
我的JSP页面(products.jsp)
<h2>List of Products</h2>
<table>
<c:forEach items="${products}" var="product">
<tr>
<td>${product.Description}</td>
<td>${product.UnitPrice}</td>
</tr> …Run Code Online (Sandbox Code Playgroud) 我想在我的JSP页面工作中应用MVC2 J2EE方法.我想在JSP中分离Servlet中的代码和设计.我面临的问题是我想在JSP页面中显示所有用户及其数据从DB表到HTML表现在我应该如何从JSP页面调用servlet,因为显示页面中没有表单我不知道是否可以使用调度程序,因为管理员将单击<a href>display users,JSP页面应显示所有uesrs.我该怎么做?
servlets ×7
jsp ×5
java ×4
html ×2
annotations ×1
forms ×1
scriptlet ×1
servlet-3.0 ×1
tomcat7 ×1