请高手帮我看下我的JAVA代码错在那里了

snh8613 2015-04-06 02:20:39
我的代码是
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<form action="${pageContext.request.contextPath}/VoteServlet?method=login"
method="post">
<table border="1" align="center">
<caption>用户登录</caption>
<tr>
<th>用户名</th>
<td><input type="text" name="username" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="提交" />
</td>
</tr>
</table>
</form>
</body>
</html>



<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!-- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
--><html>
<body>
欢迎${!empty sessionScope.user?user.username:'游客'}光临
<table border="1" align="center" width="60%">
<caption>
<h1>
候选人信息
</h1>
</caption>
<tr>
<th>姓名</th>
<th>票数</th>
<th>操作</th>
</tr>
<c:forEach var="vote" items="${requestScope.voteList}">
<tr>
<td>
<a style = "text-decoration:none"
href="${pageContext.request.contextPath}/VoteServlet?method=findVoteById&id=${vote.id}">
${vote.content}
</a>
</td>
<td>${vote.ticket}</td>
<td>
<a href="#" style = "text-decoration:none">
投票
</a>
</td>
</tr>
</c:forEach>
<tr>
<td>
<a href="/day199/VoteServlet?mthod=toLoginJsp">用户登录</a>
</td>
</tr>
</table>
</body>
</html>


package cn.itcast.web.vote.web;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.itcast.web.vote.domain.Content;
import cn.itcast.web.vote.domain.User;
import cn.itcast.web.vote.domain.Vote;
import cn.itcast.web.vote.service.VoteService;

public class VoteServlet extends HttpServlet {
private void findVoteById(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
try {
String id = request.getParameter("id");
VoteService voteService = new VoteService();
Content content = voteService.findContentById(Integer.parseInt(id));
request.setAttribute("content",content);
request.getRequestDispatcher("/WEB-INF/listContent.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
request.setAttribute("message", "根据id查询候选人信息失败");
request.getRequestDispatcher("/WEN-INF/message.jsp").forward(request, response);}}
private void findAllVote(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
try {
VoteService voteService = new VoteService();
List<Vote> voteList = voteService.findAllVote();
request.setAttribute("voteList",voteList);
request.getRequestDispatcher("/WEB-INF/listAllVote.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
request.setAttribute("message", "查询所有候选人信息失败");
request.getRequestDispatcher("/WEN-INF/message.jsp").forward(request, response);}}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String method = request.getParameter("method");
if(method != null){
if("findAllVote".equals(method)){
this.findAllVote(request, response);
}else if("findVoteById".equals(method)){
this.findVoteById(request, response);
}else if("toLoginJsp".equals(method)){
//request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request,response);
response.sendRedirect(request.getContextPath()+"/WEB-INF/login.jsp");}}}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String method = request.getParameter("method");
if(method != null){
if("login".equals(method)){
try {
this.login(request, response);
} catch (Exception e) {
e.printStackTrace();}}}}
private void login(HttpServletRequest request, HttpServletResponse response) throws Exception {
String username = request.getParameter("username");
if(username != null && username.trim().length() > 0){
User user = new User();
user.setUsername(username);
request.getSession().setAttribute("user", user);
response.sendRedirect(request.getContextPath() + "/welcome.jsp"); }}}


...全文
193 2 打赏 收藏 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
snh8613 2015-04-06
  • 打赏
  • 举报
回复
高手一下就找到要害了 非常感谢
highnewrain 2015-04-06
  • 打赏
  • 举报
回复
<a href="/day199/VoteServlet?mthod=toLoginJsp">用户登录</a>


String method = request.getParameter("method");


mthod跟method弄错了吧

51,407

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧