用javaweb写一个java的线上教学网页代码
时间: 2024-03-25 21:40:44 浏览: 104
为了方便起见,我给出一个简单的示例代码,包含了登录、注册、课程列表、课程详情等功能,仅供参考:
```java
// LoginServlet.java
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
// 验证用户名和密码是否正确
if (username.equals("admin") && password.equals("admin")) {
// 登录成功,将用户信息存储到session中
HttpSession session = request.getSession();
session.setAttribute("username", username);
// 跳转到课程列表页
response.sendRedirect("/course/list");
} else {
// 登录失败,返回错误消息
request.setAttribute("error", "用户名或密码错误");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
}
}
// RegisterServlet.java
@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
// 将用户信息存储到数据库中
// ...
// 注册成功后,跳转到登录页
response.sendRedirect("/login.jsp");
}
}
// CourseListServlet.java
@WebServlet("/course/list")
public class CourseListServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取课程列表
List<Course> courseList = getCourseList();
// 将课程列表存储到request中
request.setAttribute("courseList", courseList);
// 跳转到课程列表页
request.getRequestDispatcher("/course/list.jsp").forward(request, response);
}
}
// CourseDetailServlet.java
@WebServlet("/course/detail")
public class CourseDetailServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String courseId = request.getParameter("id");
// 根据课程id获取课程详情
Course course = getCourseById(courseId);
// 将课程详情存储到request中
request.setAttribute("course", course);
// 跳转到课程详情页
request.getRequestDispatcher("/course/detail.jsp").forward(request, response);
}
}
// Course.java
public class Course {
private String id;
private String name;
private String description;
private String imageUrl;
// 省略getter和setter方法
}
// course/list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>课程列表</title>
</head>
<body>
<h1>课程列表</h1>
<table>
<tr>
<th>课程名称</th>
<th>课程描述</th>
<th>操作</th>
</tr>
<c:forEach items="${courseList}" var="course">
<tr>
<td>${course.name}</td>
<td>${course.description}</td>
<td><a href="/course/detail?id=${course.id}">查看详情</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
// course/detail.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>课程详情</title>
</head>
<body>
<h1>${course.name}</h1>
<p>${course.description}</p>
<img src="${course.imageUrl}">
</body>
</html>
```
以上是一个简单的示例,实际情况可能更加复杂,需要根据具体需求进行调整。
阅读全文