⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simplehttpserver.html

📁 用JAVA编写的,在做实验的时候留下来的,本来想删的,但是传上来,大家分享吧
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<a name="306" href="#306">306</a> <em>     */</em><a name="307" href="#307">307</a>     <strong>public</strong> <strong>int</strong> getPort() {<a name="308" href="#308">308</a> <a name="309" href="#309">309</a>         <strong>return</strong> <strong>this</strong>.port;<a name="310" href="#310">310</a>     }<a name="311" href="#311">311</a> <a name="312" href="#312">312</a>     <em>/**<em>*</em></em><a name="313" href="#313">313</a> <em>     * @return Server reference.</em><a name="314" href="#314">314</a> <em>     */</em><a name="315" href="#315">315</a>     <strong>public</strong> HttpServer getServer() {<a name="316" href="#316">316</a> <a name="317" href="#317">317</a>         <strong>return</strong> <strong>this</strong>.server;<a name="318" href="#318">318</a>     }<a name="319" href="#319">319</a> <a name="320" href="#320">320</a>     <em>/**<em>*</em></em><a name="321" href="#321">321</a> <em>     * @param contextName Name of context to look for.  Possible names would be</em><a name="322" href="#322">322</a> <em>     * '/admin', '/', or '/selftest'.</em><a name="323" href="#323">323</a> <em>     *</em><a name="324" href="#324">324</a> <em>     * @return named context.</em><a name="325" href="#325">325</a> <em>     */</em><a name="326" href="#326">326</a>     <strong>private</strong> WebApplicationContext getContext(String contextName) {<a name="327" href="#327">327</a> <a name="328" href="#328">328</a>         WebApplicationContext context = <strong>null</strong>;<a name="329" href="#329">329</a> <a name="330" href="#330">330</a>         <strong>if</strong> (<strong>this</strong>.contexts == <strong>null</strong>)<a name="331" href="#331">331</a>         {<a name="332" href="#332">332</a>             <strong>throw</strong> <strong>new</strong> NullPointerException(<span class="string">"No contexts available."</span>);<a name="333" href="#333">333</a>         }<a name="334" href="#334">334</a> <a name="335" href="#335">335</a>         <strong>if</strong> (!contextName.startsWith(<span class="string">"/"</span>)) {<a name="336" href="#336">336</a>             contextName = '/' + contextName;<a name="337" href="#337">337</a>         }<a name="338" href="#338">338</a>         <strong>for</strong> (Iterator i = <strong>this</strong>.contexts.iterator(); i.hasNext();)<a name="339" href="#339">339</a>         {<a name="340" href="#340">340</a>             WebApplicationContext c = (WebApplicationContext)i.next();<a name="341" href="#341">341</a>             <strong>if</strong> (c.getHttpContextName().equals(contextName))<a name="342" href="#342">342</a>             {<a name="343" href="#343">343</a>                 context = c;<a name="344" href="#344">344</a>                 <strong>break</strong>;<a name="345" href="#345">345</a>             }<a name="346" href="#346">346</a>         }<a name="347" href="#347">347</a> <a name="348" href="#348">348</a>         <strong>if</strong> (context == <strong>null</strong>)<a name="349" href="#349">349</a>         {<a name="350" href="#350">350</a>             <strong>throw</strong> <strong>new</strong> NoSuchElementException(<span class="string">"Unknown webapp: "</span> + contextName);<a name="351" href="#351">351</a>         }<a name="352" href="#352">352</a> <a name="353" href="#353">353</a>         <strong>return</strong> context;<a name="354" href="#354">354</a>     }<a name="355" href="#355">355</a> <a name="356" href="#356">356</a>     <em>/**<em>*</em></em><a name="357" href="#357">357</a> <em>     * Setup a realm on the server named for the webapp and add to the</em><a name="358" href="#358">358</a> <em>     * passed webapp's context.</em><a name="359" href="#359">359</a> <em>     *</em><a name="360" href="#360">360</a> <em>     * Used by the selftest to check digest authentication is working.</em><a name="361" href="#361">361</a> <em>     * For this all to work, the &lt;code>web.xml&lt;/code> needs to set with</em><a name="362" href="#362">362</a> <em>     * a security constraint that points to a realm named for the passed</em><a name="363" href="#363">363</a> <em>     * webapp, &lt;code>webappName&lt;/code>.</em><a name="364" href="#364">364</a> <em>     *</em><a name="365" href="#365">365</a> <em>     * @param realmName Name of realm to configure.</em><a name="366" href="#366">366</a> <em>     * @param contextName Name of context we're using with this realm.</em><a name="367" href="#367">367</a> <em>     * If null, we'll use the realm name as context name.</em><a name="368" href="#368">368</a> <em>     * @param authProperties Path to file that holds the auth login and</em><a name="369" href="#369">369</a> <em>     * password.</em><a name="370" href="#370">370</a> <em>     * @return Hash of user realms.</em><a name="371" href="#371">371</a> <em>     *</em><a name="372" href="#372">372</a> <em>     * @throws IOException</em><a name="373" href="#373">373</a> <em>     */</em><a name="374" href="#374">374</a>     <strong>public</strong> HashUserRealm setAuthentication(String realmName,<a name="375" href="#375">375</a>         String contextName, String authProperties)<a name="376" href="#376">376</a>     throws IOException {<a name="377" href="#377">377</a>         HashUserRealm realm =<a name="378" href="#378">378</a>             (authProperties != <strong>null</strong> &amp;&amp; authProperties.length() > 0)?<a name="379" href="#379">379</a>                 <strong>new</strong> HashUserRealm(realmName, authProperties):<a name="380" href="#380">380</a>                 <strong>new</strong> HashUserRealm(realmName);<a name="381" href="#381">381</a>         <strong>this</strong>.server.addRealm(realm);<a name="382" href="#382">382</a>         <strong>if</strong> (contextName == <strong>null</strong> || contextName.length() &lt;= 0) {<a name="383" href="#383">383</a>             contextName = realmName;<a name="384" href="#384">384</a>         }<a name="385" href="#385">385</a>         WebApplicationContext context = getContext(contextName);<a name="386" href="#386">386</a>         context.setRealmName(realmName);<a name="387" href="#387">387</a>         <strong>return</strong> realm;<a name="388" href="#388">388</a>     }<a name="389" href="#389">389</a>     <a name="390" href="#390">390</a>     <strong>public</strong> <strong>void</strong> setAuthentication(String realmName, String contextName,<a name="391" href="#391">391</a>             String username, String password, String role)<a name="392" href="#392">392</a>     throws IOException {<a name="393" href="#393">393</a>         HashUserRealm realm = setAuthentication(realmName, contextName,<a name="394" href="#394">394</a>             <strong>null</strong>);<a name="395" href="#395">395</a>         realm.put(username, password);<a name="396" href="#396">396</a>         realm.addUserToRole(username, role);<a name="397" href="#397">397</a>     }<a name="398" href="#398">398</a>     <a name="399" href="#399">399</a> <a name="400" href="#400">400</a>     <em>/**<em>*</em></em><a name="401" href="#401">401</a> <em>     * Reset the administrator login info. </em><a name="402" href="#402">402</a> <em>     * </em><a name="403" href="#403">403</a> <em>     * @param realmAndRoleName for our use, always 'admin'</em><a name="404" href="#404">404</a> <em>     * @param oldUsername previous username to replace/disable</em><a name="405" href="#405">405</a> <em>     * @param newUsername new username (may be same as old)</em><a name="406" href="#406">406</a> <em>     * @param newPassword new password</em><a name="407" href="#407">407</a> <em>     */</em><a name="408" href="#408">408</a>     <strong>public</strong> <strong>void</strong> resetAuthentication(String realmAndRoleName,<a name="409" href="#409">409</a>         String oldUsername, String newUsername, String newPassword) {<a name="410" href="#410">410</a>         HashUserRealm realm = (HashUserRealm)<strong>this</strong>.server.<a name="411" href="#411">411</a>             getRealm(realmAndRoleName);<a name="412" href="#412">412</a>         realm.remove(oldUsername);<a name="413" href="#413">413</a>         realm.put(newUsername,newPassword);<a name="414" href="#414">414</a>         realm.addUserToRole(newUsername, realmAndRoleName);<a name="415" href="#415">415</a>     }<a name="416" href="#416">416</a> <a name="417" href="#417">417</a>     <em>/**<em>*</em></em><a name="418" href="#418">418</a> <em>     * Get path to named webapp.</em><a name="419" href="#419">419</a> <em>     *</em><a name="420" href="#420">420</a> <em>     * @param name Name of webpp.  Possible names are 'admin' or 'selftest'.</em><a name="421" href="#421">421</a> <em>     *</em><a name="422" href="#422">422</a> <em>     * @return Path to deployed webapp.</em><a name="423" href="#423">423</a> <em>     */</em><a name="424" href="#424">424</a>     <strong>public</strong> File getWebappPath(String name) {<a name="425" href="#425">425</a> <a name="426" href="#426">426</a>         <strong>if</strong> (<strong>this</strong>.server == <strong>null</strong>) {<a name="427" href="#427">427</a>             <strong>throw</strong> <strong>new</strong> NullPointerException(<span class="string">"Server does not exist"</span>);<a name="428" href="#428">428</a>         }<a name="429" href="#429">429</a>         String contextName =<a name="430" href="#430">430</a>             (name.equals(<strong>this</strong>.server.getRootWebApp()))? <span class="string">"/"</span>: <span class="string">"/"</span> + name;<a name="431" href="#431">431</a>         <strong>return</strong> <strong>new</strong><a name="432" href="#432">432</a>             File(getContext(contextName).getServletHandler().getRealPath(<span class="string">"/"</span>));<a name="433" href="#433">433</a>     }<a name="434" href="#434">434</a> <a name="435" href="#435">435</a>     <em>/**<em>*</em></em><a name="436" href="#436">436</a> <em>     * @return Returns the root webapp name.</em><a name="437" href="#437">437</a> <em>     */</em><a name="438" href="#438">438</a>     <strong>public</strong> <strong>static</strong> String getRootWebappName()<a name="439" href="#439">439</a>     {<a name="440" href="#440">440</a>         <strong>return</strong> ROOT_WEBAPP;<a name="441" href="#441">441</a>     }<a name="442" href="#442">442</a>     <a name="443" href="#443">443</a>     <a name="444" href="#444">444</a>     <em>/**<em>*</em></em><a name="445" href="#445">445</a> <em>     * Returns the hosts that the server is listening on.</em><a name="446" href="#446">446</a> <em>     * </em><a name="447" href="#447">447</a> <em>     * @return  the hosts that the server is listening on.</em><a name="448" href="#448">448</a> <em>     */</em><a name="449" href="#449">449</a>     <strong>public</strong> Collection&lt;String> getHosts() {<a name="450" href="#450">450</a>         ArrayList&lt;String> result = <strong>new</strong> ArrayList&lt;String>();<a name="451" href="#451">451</a>         <strong>for</strong> (HttpListener listener: server.getListeners()) {<a name="452" href="#452">452</a>             result.add(listener.getHost());<a name="453" href="#453">453</a>         }<a name="454" href="#454">454</a>         <strong>return</strong> result;<a name="455" href="#455">455</a>     }<a name="456" href="#456">456</a> }</pre><hr/><div id="footer">This page was automatically generated by <a href="http://maven.apache.org/">Maven</a></div></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -