注:本文转自《白煮蛋的博客》
我将向您展示一个 servlet 的完整示例以及如何进行 Ajax 调用。
在这里,我们将创建一个简单的示例来使用 servlet 创建登录表单。
文件index.html
<span style="background-color:var(--highlight-bg)"><span style="color:var(--highlight-color)"><code class="language-xml"><<span style="color:var(--highlight-namespace)">form</span>>
Name:<<span style="color:var(--highlight-namespace)">input</span> <span style="color:var(--highlight-attribute)">type</span>=<span style="color:var(--highlight-variable)">"text"</span> <span style="color:var(--highlight-attribute)">name</span>=<span style="color:var(--highlight-variable)">"username"</span>/><<span style="color:var(--highlight-namespace)">br</span>/><<span style="color:var(--highlight-namespace)">br</span>/>
Password:<<span style="color:var(--highlight-namespace)">input</span> <span style="color:var(--highlight-attribute)">type</span>=<span style="color:var(--highlight-variable)">"password"</span> <span style="color:var(--highlight-attribute)">name</span>=<span style="color:var(--highlight-variable)">"userpass"</span>/><<span style="color:var(--highlight-namespace)">br</span>/><<span style="color:var(--highlight-namespace)">br</span>/>
<<span style="color:var(--highlight-namespace)">input</span> <span style="color:var(--highlight-attribute)">type</span>=<span style="color:var(--highlight-variable)">"button"</span> <span style="color:var(--highlight-attribute)">value</span>=<span style="color:var(--highlight-variable)">"login"</span>/>
</<span style="color:var(--highlight-namespace)">form</span>>
</code></span></span>
Ajax 示例
<span style="background-color:var(--highlight-bg)"><span style="color:var(--highlight-color)"><code class="language-javascript">$.ajax
({
<span style="color:var(--highlight-attribute)">type</span>: <span style="color:var(--highlight-variable)">"POST"</span>,
<span style="color:var(--highlight-attribute)">data</span>: <span style="color:var(--highlight-variable)">'LoginServlet='</span> + name + <span style="color:var(--highlight-variable)">'&name='</span> + type + <span style="color:var(--highlight-variable)">'&pass='</span> + password,
<span style="color:var(--highlight-attribute)">url</span>: url,
<span style="color:var(--highlight-attribute)">success</span>:<span style="color:var(--highlight-keyword)">function</span>(content)
{
$(<span style="color:var(--highlight-variable)">'#center'</span>).<span style="color:var(--highlight-literal)">html</span>(content);
}
});
</code></span></span>
LoginServlet servlet 代码:
<span style="background-color:var(--highlight-bg)"><span style="color:var(--highlight-color)"><code class="language-java"><span style="color:var(--highlight-keyword)">package</span> abc.servlet;
<span style="color:var(--highlight-keyword)">import</span> java.io.File;
<span style="color:var(--highlight-keyword)">public</span> <span style="color:var(--highlight-keyword)">class</span> <span style="color:var(--highlight-literal)">AuthenticationServlet</span> <span style="color:var(--highlight-keyword)">extends</span> <span style="color:var(--highlight-literal)">HttpServlet</span> {
<span style="color:var(--highlight-keyword)">private</span> <span style="color:var(--highlight-keyword)">static</span> <span style="color:var(--highlight-keyword)">final</span> <span style="color:var(--highlight-namespace)">long</span> <span style="color:var(--highlight-variable)">serialVersionUID</span> = <span style="color:var(--highlight-namespace)">1L</span>;
<span style="color:var(--highlight-keyword)">protected</span> <span style="color:var(--highlight-keyword)">void</span> <span style="color:var(--highlight-literal)">doGet</span>(HttpServletRequest request, HttpServletResponse response)
<span style="color:var(--highlight-keyword)">throws</span> ServletException, IOException
{
doPost(request, response);
}
<span style="color:var(--highlight-keyword)">protected</span> <span style="color:var(--highlight-keyword)">void</span> <span style="color:var(--highlight-literal)">doPost</span>(HttpServletRequest request,
HttpServletResponse response)
<span style="color:var(--highlight-keyword)">throws</span> ServletException, IOException {
<span style="color:var(--highlight-keyword)">try</span>{
<span style="color:var(--highlight-namespace)">HttpSession</span> <span style="color:var(--highlight-variable)">session</span> = request.getSession();
<span style="color:var(--highlight-namespace)">String</span> <span style="color:var(--highlight-variable)">username</span> = request.getParameter(<span style="color:var(--highlight-variable)">"name"</span>);
<span style="color:var(--highlight-namespace)">String</span> <span style="color:var(--highlight-variable)">password</span> = request.getParameter(<span style="color:var(--highlight-variable)">"pass"</span>);
<span style="color:var(--highlight-comment)">/// Your Code</span>
out.println(<span style="color:var(--highlight-variable)">"sucess / failer"</span>)
}
<span style="color:var(--highlight-keyword)">catch</span> (Exception ex) {
<span style="color:var(--highlight-comment)">// System.err.println("Initial SessionFactory creation failed.");</span>
ex.printStackTrace();
System.exit(<span style="color:var(--highlight-namespace)">0</span>);
}
}
}</code></span></span>