JAVAEE-JSP
一:新建javaweb工程
ps:可能有的编译器新建之后,是以下样式:
就是在WEB-INF下面没有web.xml
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" id="WebApp_ID" version="3.0">
<display-name>d</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>
</web-app>
也可以直接在新建的时候,勾选一下,即可自动创建
二:创建login实例
1-1:配置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" id="WebApp_ID" version="3.0">
<display-name>2017-06-27loginTest</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>login</servlet-name>
<servlet-class>com.control.LoginPage</servlet-class>
</servlet>
<servlet>
<servlet-name>logincheck</servlet-name>
<servlet-class>com.control.LoginCheck</servlet-class>
</servlet>
<servlet>
<servlet-name>index</servlet-name>
<servlet-class>com.control.indexPage</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login.action</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>logincheck</servlet-name>
<url-pattern>/loginCheck.action</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>index</servlet-name>
<url-pattern>/indexPage.action</url-pattern>
</servlet-mapping>
</web-app>
login
//serlvet-name:servlet的名称
com.control.LoginPage
//serlvet-class:servlet对应的class,包名之间用.隔开
login //serlvet-name:servlet的名称 /login.action //url-pattern:servlet跳转的路径url **重点:**两个servlet-name要相同
1-2:login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<%
String path = request.getContextPath();
String msg=request.getParameter("msg");
%>
<!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>
<form action="<%=path%>/loginCheck.action">
账号:<input type="text" name="userId"> <br/>
密码:<input type="text" name="pwd"><br/>
<button type="submit">提交</button>
</form>
</body>
</html>
1-3:index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String id=request.getParameter("userId");
String pwd=request.getParameter("pwd");
String msg=(String)request.getAttribute("msg");
System.out.println("==user:"+id+":"+"pwd:"+pwd+" msg:"+msg);
%>
<!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>
你好:<%=id %>,<%=pwd %>,<%=msg %>
</body>
</html>
对应的后端java处理代码:
2-1:LoginPage.java
package com.control;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginPage extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setAttribute("msg", "下大雨了");
req.getRequestDispatcher("/login.jsp").forward(req, resp);
}
}
2-2:LoginCheck.java
package com.control;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginCheck extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String id=req.getParameter("userId");
String pwd=req.getParameter("pwd");
System.out.println("id:"+id);
System.out.println("pwd:"+pwd);
if(id.trim().equals("123")&&pwd.trim().equals("222"))
{
pwd=pwd+id;
System.out.println("里面的id:"+id);
System.out.println("里面的pwd:"+pwd);
req.setAttribute("user", id);
req.setAttribute("pwd", pwd);
req.getRequestDispatcher("/indexPage.action").forward(req, resp);
}else
{
req.getRequestDispatcher("/login.action").forward(req, resp);
}
}
}
2-3:indexPage.java
package com.control;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class indexPage extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setAttribute("msg", "123");
req.getRequestDispatcher("/index.jsp").forward(req, resp);
}
}