jsp连接servlet配置web.xml
两种方法
第一种:建立jsp文件,servlet文件,配置web.xml文件。
第二种:建立jsp文件,servlet文件。
首先介绍第一种:
打开eclipse,选中javaee模式
建立一个Dynamic Web Project
在WebContent下建一个jsp文件,lib下放jar包。
在创建项目的时候可能忘记添加xml文件,可以在项目建好后添加。
在NewFile.jsp中的代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="/Test/Test">A标签点我跳转到servlet</a>
</body>
</html>
在src目录下建一个servlet包,在包下建一个Test.java
Test 实现servlet 重写servlet方法,如果跳转到Test就会执行service方法中的代码
package servlet;
import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class Test implements Servlet {
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getServletInfo() {
// TODO Auto-generated method stub
return null;
}
@Override
public void init(ServletConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
@Override
public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("111");
}
}
web.xml文件中写入以下代码。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>Test</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Test</servlet-name>
<servlet-class>servlet.Test</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/Test</url-pattern>
</servlet-mapping>
</web-app>
贴上图片解释
运行一下
点击之后
进入Test.java
控制台打印111表明进入servlet成功,就可在servlet中执行登录注册转发重定向,得到jsp的参数的值,把值传入到dao包下的userdao中具体的crud(增删改查)。判读crud方法返回的值判断一下转发,重定向到某一个页面。转发可以用到登录
request.setArrtu…。重定向无法存作用域。
第二种方法
不需要web.xml
在servlet包下建立Test2.java 他extends HttpServlet 重写doget,dopost,(alt+/快捷键可打出)
package 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("/Test21")
public class Test2 extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//System.out.println("doGet方法");
this.doPost(req, resp); // 把所有的get方法转到post方法
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("dopost111方法");
}
}
在WebContent 下建立NewFile1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="/Test/Test21">第二种方法跳转</a>
<form action ="/Test/Test2" method="get">
<input type="submit" value="提交">
</form>
</body>
</html>
用第二种方式就不需要web.xml。
可能会报错导入这个包手动导入servlet-api.jar
有些配置完web.xml可能需要重启一下eclipse。