静态页面如何实现跳转到jsp页面
时间: 2023-11-18 17:05:50 浏览: 32
可以使用<a>标签或<form>标签中的<button>标签设置跳转链接或表单提交到jsp页面,如下所示:
<a href="yourJSPPage.jsp">跳转到JSP页面</a>
或
<form action="yourJSPPage.jsp" method="post">
<button type="submit">跳转到JSP页面</button>
</form>
相关问题
编写一个首页,该页面能够自动跳转到WelcomeServlet
假设您正在使用Java Servlet技术,可以按照以下步骤编写首页:
1. 创建一个名为index.html的静态HTML文件,其中包含以下内容:
```
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Home Page</title>
</head>
<body>
<script>
window.location.href = "WelcomeServlet";
</script>
</body>
</html>
```
2. 创建一个名为WelcomeServlet的Java Servlet类,该类将处理跳转请求:
```
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/WelcomeServlet")
public class WelcomeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.sendRedirect("welcome.jsp");
}
}
```
在此示例中,我们使用了@WebServlet注释来指定Servlet的URL模式。在doGet方法中,我们使用HttpServletResponse对象的sendRedirect方法将请求重定向到welcome.jsp页面。
3. 创建一个名为welcome.jsp的JSP页面,以显示欢迎消息:
```
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Welcome</title>
</head>
<body>
<h1>Welcome to my website!</h1>
</body>
</html>
```
在此示例中,我们简单地显示了一个欢迎消息。
4. 部署您的Web应用程序,并在浏览器中打开首页。您应该会看到自动跳转到WelcomeServlet,并显示欢迎消息。
在eclipse实现maven登录页面代码
这里给出一个使用 Maven 和 SpringMVC 实现登录页面的示例代码,你可以在 Eclipse 中创建一个 Maven 项目,然后按照以下步骤进行操作:
1. 在 pom.xml 文件中添加 SpringMVC 和 JSP 相关依赖:
```xml
<dependencies>
<!-- SpringMVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.9</version>
</dependency>
<!-- JSP -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>9.0.50</version>
</dependency>
</dependencies>
```
2. 创建一个 UserController 类,在其中添加登录方法:
```java
package com.example.demo.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class UserController {
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String showLoginPage() {
return "login";
}
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String handleLoginRequest(HttpServletRequest request) {
String username = request.getParameter("username");
String password = request.getParameter("password");
if ("admin".equals(username) && "123456".equals(password)) {
return "home";
} else {
return "login";
}
}
}
```
3. 在 src/main/resources 目录下创建 SpringMVC 配置文件 springmvc.xml,并配置视图解析器和资源路径:
```xml
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<mvc:resources mapping="/assets/**" location="/assets/" />
```
4. 在 src/main/webapp/WEB-INF/views 目录下创建 login.jsp 和 home.jsp 两个 JSP 页面,用于显示登录表单和登录成功页面,代码如下:
login.jsp:
```html
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<h1>Login Page</h1>
<form action="login" method="post">
<table>
<tr>
<td>Username:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Login"></td>
</tr>
</table>
</form>
</body>
</html>
```
home.jsp:
```html
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Home Page</title>
</head>
<body>
<h1>Welcome <%=request.getParameter("username") %>!</h1>
<a href="<c:url value="/login" />">Logout</a>
</body>
</html>
```
5. 在 src/main/webapp 目录下创建 assets 目录,并将样式表和图片等静态资源放到该目录下。
6. 配置 Tomcat 服务器并启动项目,在浏览器中访问 http://localhost:8080/login 即可看到登录页面。输入正确的用户名和密码后,将跳转到 home 页面,显示欢迎信息。