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

snh8613 snh8613 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我的代码是
<%@ 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"); }}}


...全文

相关推荐

JAVA分页源代码需要可以看下

分页源码分页源码分页源码分页源码分页源码分页源码分页源码分页源码分页源码分页源码

MichaelKoo

国外的Java游戏源代码

一个国外的j2me游戏源码,源自JAva核心,游戏写的是什么,没有测试运行,好多文件夹,源码结构规整,请在Eclipse下运行项目,学习Java研究Android软件开发的,可要借机看下,必竟两者的差别不太远,很多知识是共有的。 运行环境:Java/Eclipse

weixin_38694541

小学家校一体“作业帮”系统代码 java小学家校一体“作业帮”系统代码

小学家校一体“作业帮”系统代码 java小学家校一体“作业帮”系统代码 基于springboot的小学家校一体“作业帮”系统代码 1、小学家校一体“作业帮”系统的技术栈、环境、工具、软件: ① 系统环境:Windows/Mac ② 开发语言:Java ③ 框架:SpringBoot ④ 架构:B/S、MVC ⑤ 开发环境:IDEA、JDK、Maven、Mysql ⑥ JDK版本:JDK1.8 ⑦ Maven包:Maven3.6 ⑧ 数据库:mysql 5.7 ⑨ 服务平台:Tomcat 8.0/9.0 ⑩ 数据库工具:SQLyog/Navicat ⑪ 开发软件:eclipse/myeclipse/idea ⑫ 浏览器:谷歌浏览器/微软edge/火狐 ⑬ 技术栈:Java、Mysql、Maven、Springboot、Mybatis、Ajax、Vue等 2、适用人群:计算机,电子信息工程等专业的学习者等, 高分毕业设计项目,也可作为课程设计和期末大作业。本资源仅是代码的压缩包,该代码适合毕业设计、课程设计作业,所有源码均经过严格测试,可以放心下载使用。有任何使用问题欢迎随时与博主沟

我叫岩雨

Java游戏代码自己看下

Java游戏代码 可以自己在调试一下酒好了 最好在Vc++下调试

吴新强

ChatGPT帮你完成Java类代码 场景

ChatGPT帮你完成Java类代码 场景 如何利用chatGPT高效快速完成Java 类代码编写

传说三哥
评论 2
收藏 已收藏
分享
返回顶部
snh8613
Java 51,407 成员 86,129 内容