编写一个servlet来添加/编辑/删除一个项以及一个列表项
【腾讯云】亏本大甩卖,服务器4核16G 1年370元(带宽12M,系统盘120GB SSD盘,月流量2000GB)!!!!!!
云产品 配置 价格
服务器 1核2G,带宽5M,系统盘50GB SSD盘,月流量500GB 38元/年
MySQL 1核1G 19元/年
服务器 16核32G,带宽18M,系统盘250GB SSD盘,月流量5000GB 1197元/年
点我进入腾讯云,查看更多详情

I need to create a single Servlet to handle listing of some Items, adding new items, editing existing items and also deletion of items.

I wrote JSP pages for edit an item and showing a list of items. In itemslisting.jsp, I have put delete and edit links for each item but I am not very sure about how to construct the methods in Servlet.

itemslisting.jsp

...
<table>
<c:forEach var="item" items="${items}" varStatus="listCount">

<tr><td>${listCount.count} </td>
<c:url value="edititem" var="edititemurl">

    <c:param name="itemId" value="${item.item_id}" />

</c:url>
<td><a href="${edititemurl }" >${item.name}</a></td>
<c:url value="deleteitem" var="deleteitemurl">

    <c:param name="itemId" value="${item.item_id}" />

</c:url>

<td><a href="${deleteitemurl}" >DELETE</a></td>

</tr>

</c:forEach>

</table>
...
<a href="<c:url value="addItem"/>">add new item </a>
...

In our training we were asked to create separate servlets to add, edit, list and delete Items. In those cases mapping can be done for each of these urls to the appropriate servlet but it feels silly to create that many servlets. How do I configure the different urls in the above jsp to a single ItemServlet? Do I have to pass some extra parameter to indicate the operation, say like edititem along with the edititemurl?

I tried to create a skeleton of the doGet() method:

public void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
    String additemop = req.getParameter("additemoperation");
    String edititem = req.getParameter("additemoperation");
    String itemId =  req.getParameter("itemId");
    ...
    if(additemop!=null){
      showAddItemPage(req,resp);
    }
    else if((edititemop!=null) && (itemId != null)){
      showEditItemPage(req,resp);
    }
    else{
      showListItemsPage(req,resp);
    }

}

I don't know if this is the way to do this, please correct me if I am wrong.

#0

You can pass a parameter with your url, like:

myapp/item?action=get

Using separate urls for these is easier and less error-prone, though. That is likely why you were taught that way in the first place.

You need to be careful what you do with HTTP GET and what you do with POST. Using GET like you do in your example (to add an item) is bad, you don't want to do anything that modifies data with GET. Especially don't make deletion a GET, or the first webcrawler that comes along can wipe out your database. :-O Also check out something called Post-Redirect-Get, it is a common pattern for avoiding duplicate form submissions and making your pages bookmarkable.

I recommend checking out a rapid-development framework, like Play. The raw jsp/servlet stuff here is just too tedious to grind out. You have already been through the hard way once, so you won't be missing out on anything.

推荐文章

使用nsihttpchannel监视传出请求正文

使用nsihttpchannel监视传出请求正文

推荐文章

特定服务器上php函数mail的附加头问题

特定服务器上php函数mail的附加头问题

推荐文章

perl—处理许多类似命名的文本文件的最佳方法

perl—处理许多类似命名的文本文件的最佳方法

推荐文章

jQuery发送字符串作为POST参数

jQuery发送字符串作为POST参数

推荐文章

在Matlab中处理不同格式的日期字符串

在Matlab中处理不同格式的日期字符串

推荐文章

用AJAX加载外部页面的最佳方法

用AJAX加载外部页面的最佳方法

推荐文章

计算器中只有一个逗号/点!iPhone的Objective-C

计算器中只有一个逗号/点!iPhone的Objective-C

推荐文章

为什么RabbitMQ不在持久队列上持久化消息?

为什么RabbitMQ不在持久队列上持久化消息?

推荐文章

Zend框架PHP中页面的基本流程

Zend框架PHP中页面的基本流程

推荐文章

无法使用RVM安装Webiva

无法使用RVM安装Webiva

推荐文章

处理开发、测试和生产之间的sharepoint工作流差异

处理开发、测试和生产之间的sharepoint工作流差异

推荐文章

在OL下对LI命令排序

在OL下对LI命令排序

推荐文章

UITableViewCell在iPhone和iPad上的工作方式不同

UITableViewCell在iPhone和iPad上的工作方式不同

推荐文章

从bookmarklet将文本复制到剪贴板

从bookmarklet将文本复制到剪贴板

推荐文章

在本地主机上执行HTTPS请求时出现问题

在本地主机上执行HTTPS请求时出现问题

推荐文章

如何在控制台中读取pulseaudio客户端的音量级别

如何在控制台中读取pulseaudio客户端的音量级别