请帮助我,我不能在第3天这样做((
这是我的web.xml文件
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>ebookshopservlet</servlet-name>
<servlet-class>
ebookshop.ShoppingServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ebookshopservlet</servlet-name>
<url-pattern>/eshop</url-pattern>
</servlet-mapping>
</web-app>
我有jsp项目,并且/ ebookshop / eshop网址存在问题,它不断写入404错误 - 未找到。配置此Web服务器和应用程序时我的错误是什么?
有我的servlet
package ebookshop;
import java.util.Vector;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import ebookshop.Book;
import javax.servlet.annotation.WebServlet;
/**
*
* @author Ilqar
*/
public class ShoppingServlet extends HttpServlet{
public void init(ServletConfig conf) throws ServletException {
super.init(conf);
}
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
doPost(req,res);
}
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
// @WebServlet(name = "ShoppingServlet", urlPatterns = {"/eshop"})
HttpSession session;
session = req.getSession();
@SuppressWarnings("unchecked")
Vector<Book> shoplist=(Vector<Book>) session.getAttribute("ebookshop.cart");
String do_this=req.getParameter("do_this");
if (do_this==null)
{
Vector<String> blist=new Vector<String>();
blist.addElement("Learn HTML5 and JavaScript for iOS. Scott Preston $39.99");
blist.addElement("Java 7 for Absolute Beginners. Jay Bryant $39.99");
blist.addElement("Beginning Android 4. Livingston $39.99");
blist.addElement("Pro Spatial with SQL Server 2012. Alastair Aitchinson $59.99");
blist.addElement("Beginning Database Design. Clare Churcher $34.99");
session.setAttribute("ebookshop.list", blist);
ServletContext sc=getServletContext();
RequestDispatcher rd=sc.getRequestDispatcher("/");
rd.forward(req, res);
}
else {
if (do_this.equals("checkout"))
{
float dollars=0;
int books=0;
for (Book abook: shoplist)
{
dollars+=abook.getPrice()*abook.getQuantity();
books+=abook.getQuantity();
}
req.setAttribute("dollars", new Float(dollars).toString());
req.setAttribute("books", new Integer(books).toString());
ServletContext sc=getServletContext();
RequestDispatcher rd=sc.getRequestDispatcher("/Checkout.jsp");
rd.forward(req, res);
} // end if (do_this.equals("checkout"))
else {
if (do_this.equals("remove")){
String pos=req.getParameter("position");
shoplist.removeElementAt((new Integer(pos)));
}
else if (do_this.equals("add"))
{
boolean found=false;
Book abook=getBook(req);
if (shoplist==null)
{
shoplist=new Vector<Book>();
shoplist.addElement(abook);
}
else
{
for (int i=0;i<shoplist.size() && !found;i++)
{
Book b= (Book) shoplist.elementAt(i);
if (b.getTitle().equals(abook.getTitle()))
{
b.setQuantity(b.getQuantity()+abook.getQuantity());
shoplist.setElementAt(b, i);
found=true;
}
}
if (!found)
{
shoplist.addElement(abook);
}
}
}
session.setAttribute("ebookshop.cart", shoplist);
ServletContext sc=getServletContext();
RequestDispatcher rd=sc.getRequestDispatcher("/");
rd.forward(req, res);
}
}
}
public Book getBook(HttpServletRequest req){
String myBook=req.getParameter("book");
int n=myBook.indexOf("$");
String name=myBook.substring(0, n);
String price=myBook.substring(n+1);
String qty=req.getParameter("qty");
return new Book(name,Float.parseFloat(price),Integer.parseInt(qty));
}
}
和我的index.jsp文件
<%--
Document : ebookshop_index
Created on : 22.07.2015, 21:33:24
Author : Ilqar
--%>
<%@page contentType="text/html" pageEncoding="UTF-8" trimDirectiveWhitespaces="true"%>
<%@page session="true" import="java.util.*,ebookshop.Book" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>E-bookshop</title>
<style type="text/css">
body {background-color: gray; font-size: 10pt;}
h1 {font-size: 20pt}
table {background-color: white;}
</style>
</head>
<body>
<h1>Your online Bookshop!</h1>
<% //Scriplet 1 checks whether the booklist is ready
Vector<ebookshop.Book> booklist=(Vector<ebookshop.Book>)session.getAttribute("ebookshop.list");
if (booklist==null)
{
response.sendRedirect("/ebookshop/eshop");
}
else {
%>
<form name="addForm" method="POST" action="eshop">
<input type="hidden" name="do_this" value="add">
Book:
<select name="book">
<% //Scriplet the booklist to the selection control
for (int i=0;i<booklist.size();i++)
{
out.println("<option>"+booklist.elementAt(i).getTitle()+"</option>");
}
%>
</select>
Quantity: <input type="text" name="qty" value="1" size="3">
<input type="submit" value="Add to Cart">
</form>
<p/>
<% //Scriplet 3 check whether the shopping cart is empty
Vector shoplist=
(Vector<ebookshop.Book>)session.getAttribute("ebookshop.cart");
if ((shoplist!=null) && (shoplist.size()>0)){
%>
<table style="border: 1px; padding: 2px">
<tr>
<td>TITLE</td>
<td>PRICE</td>
<td>QUANTITY</td>
<td></td>
</tr>
<% //Scriplet show the books in shopping cart
for (int i=0;i<shoplist.size();i++)
{
Book aBook=(Book)shoplist.elementAt(i);
%>
<tr>
<form name="removeForm" action="eshop" method="POST">
<input type="hidden" name="position" value="<%=i%>">
<input type="hidden" name="do_this" value="remove">
<td>
<%=aBook.getTitle()%>
</td>
<td style="text-align: right">
<%=aBook.getPrice()%>
</td>
<td style="text-align: right">
<%=aBook.getQuantity()%>
</td>
</form>
</tr>
<%
}
%>
</table>
<p/>
<form name="checkoutForm" action="eshop" method="POST">
<input type="hidden" value="checkout" name="do_this">
<input type="submit" value="Checkout">
</form>
<%
}
}
%>
</body>
</html>
我猜它会在session属性为null时将重定向发送到servlet
答案 0 :(得分:0)
有效!我忘了在servlett类之前添加注释,这就是它无法找到页面的原因。这本书的作者没有提出,可能因为它没有被使用过。我只是把
@WebServlet(name =&#34; ShoppingServlet&#34;,urlPatterns = {&#34; / eshop&#34;})
和标准web.xml,一切正常!我使用过Tomcat服务器,并希望它也适用于Glassfish。