随笔笔记
1.超链接输出内容:a标签调用后台的servlet
test.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>test servlet</title>
</head>
<body>
<a href="test.do">testservlet</a>
</body>
</html>
TestServlet.java
package cn.jiangzuo.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter out=resp.getWriter();
out.println("success");
}
}
点击超链接后返回效果:
2.超链接输出内容:a标签传值并输出
test.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>test servlet</title>
</head>
<body>
<a href="test.do?user=jiangzuo&address=luoyang">testservlet</a>
</body>
</html>
TestServlet.java
package cn.jiangzuo.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
String username=req.getParameter("user");
String useraddress=req.getParameter("address");
PrintWriter out=resp.getWriter();
out.println(username+useraddress);
}
}
点击超链接后返回效果:
1和2的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>lianxi</display-name>
<!-- 测试servlet -->
<servlet>
<servlet-name>testServlet</servlet-name>
<servlet-class>cn.jiangzuo.servlet.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>testServlet</servlet-name>
<url-pattern>/test.do</url-pattern>
</servlet-mapping>
<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>
3.form表单传值
form.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>form testing</title>
</head>
<body>
<form action="login.do" method="post">
<label>username</label><input type="text" name="username"/><br/>
<label>password</label><input type="password" name="password"/><br/>
<input type="submit" value="login"/>
<input type="reset" value="reset"/>
</form>
</body>
</html>
LoginServlet.java
package cn.jiangzuo.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
String username=req.getParameter("username");
String password=req.getParameter("password");
PrintWriter out=resp.getWriter();
out.write(username+password);
}
}
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>lianxi</display-name>
<!-- 测试servlet -->
<servlet>
<servlet-name>testServlet</servlet-name>
<servlet-class>cn.jiangzuo.servlet.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>testServlet</servlet-name>
<url-pattern>/test.do</url-pattern>
</servlet-mapping>
<!-- 配置loginservlet -->
<servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>cn.jiangzuo.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/login.do</url-pattern>
</servlet-mapping>
<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>
运行结果